perm filename BOOK.XGP[BOO,JMC]2 blob sn#488055 filedate 1979-09-17 generic text, type T, neo UTF8
/LMAR=0/XLINE=3/FONT#0=BAXL30[FNT,CLT]/FONT#1=BAXM30/FONT#2=BAXB30[FNT,CLT]/FONT#3=SUB/FONT#4=SUP/FONT#5=GACS25/FONT#6=FIX20/FONT#7=SYMB30[FNT,CLT]/FONT#8=FIX25/FONT#11=GRFX25/FONT#12=GRFX35/FONT#9=BEESIX















␈↓ ↓H␈↓	␈↓ ε≥LISP


␈↓ ↓H␈↓	␈↓ ∧eProgramming and Proving














␈↓ ↓H␈↓␈↓ ¬SCopyright ␈↓π@␈↓ 1979

␈↓ ↓H␈↓␈↓ ∧XJohn McCarthy and Carolyn Talcott


␈↓ ↓H␈↓␈↓ ¬GStanford University



␈↓ ↓H␈↓␈↓ βwThis version printed at 3:53 on September 17, 1979.
␈↓ ↓H␈↓␈↓ εH␈↓ ?i


␈↓ ↓H␈↓α␈↓ ¬NTable of Contents



␈↓ ↓H␈↓␈↓ 	Page



␈↓ ↓H␈↓INTRODUCTION


␈↓ ↓H␈↓I␈↓ α_INTRODUCTION TO LISP

␈↓ ↓H␈↓␈↓ α81␈↓ αxLists.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ ≠   1

␈↓ ↓H␈↓␈↓ α82␈↓ αxAtoms.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ ≠   2

␈↓ ↓H␈↓␈↓ α83␈↓ αxList structures.␈↓ ∧8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ ≠   3

␈↓ ↓H␈↓␈↓ α84␈↓ αxS-expressions.␈↓ ∧8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ ≠   4

␈↓ ↓H␈↓␈↓ α85␈↓ αxThe basic functions and predicates on S-expressions.␈↓ λ8   .   .   .   .   .   .   .   .   .␈↓ ≠   7

␈↓ ↓H␈↓␈↓ α86␈↓ αxConditional terms.␈↓ ∧x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    10

␈↓ ↓H␈↓␈↓ α87␈↓ αxPropositional terms.␈↓ ∧x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    13

␈↓ ↓H␈↓␈↓ α88␈↓ αxRecursive function definitions.␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    14

␈↓ ↓H␈↓␈↓ α89␈↓ αxNumerical computation.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    19

␈↓ ↓H␈↓␈↓ α810␈↓ αxBitwise Boolean Operations␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    21

␈↓ ↓H␈↓␈↓ α811␈↓ αxLambda expressions and functions with functions as arguments.␈↓ 	8   .   .   .   .   .␈↓    23

␈↓ ↓H␈↓␈↓ α812␈↓ αxLabel.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    26

␈↓ ↓H␈↓␈↓ α813␈↓ αxThe function ␈↓↓eval.␈↓␈↓ ∧x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    27


␈↓ ↓H␈↓II␈↓ α_WRITING RECURSIVE FUNCTION DEFINITIONS

␈↓ ↓H␈↓␈↓ α81␈↓ αxStatic and dynamic ways of programming.␈↓ π8   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    31

␈↓ ↓H␈↓␈↓ α82␈↓ αxRecursive definition of functions on natural numbers.␈↓ λ8   .   .   .   .   .   .   .   .   .␈↓    32

␈↓ ↓H␈↓␈↓ α83␈↓ αxSimple list recursion.␈↓ ∧x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    34

␈↓ ↓H␈↓␈↓ α84␈↓ αxSimple S-expression recursion.␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    36

␈↓ ↓H␈↓␈↓ α85␈↓ αxOther structural recursions.␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    38
␈↓ ↓H␈↓ii␈↓ ¬RTable of Contents␈↓ H


␈↓ ↓H␈↓␈↓ α86␈↓ αxGeneral tree recursion.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    39

␈↓ ↓H␈↓␈↓ α87␈↓ αxSolving a LISP programming problem.␈↓ εx   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    40

␈↓ ↓H␈↓␈↓ α88␈↓ αxLots of LISP functions to program.␈↓ ε8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    44


␈↓ ↓H␈↓III␈↓ α_PROVING FACTS ABOUT LISP PROGRAMS

␈↓ ↓H␈↓␈↓ α81␈↓ αxAbout Formalizing.␈↓ ∧x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    50

␈↓ ↓H␈↓␈↓ α82␈↓ αxThe theory of S-expressions.␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    51

␈↓ ↓H␈↓␈↓ α83␈↓ αxSummary of notion of formal theory.␈↓ εx   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    55

␈↓ ↓H␈↓␈↓ α84␈↓ αxLists, natural numbers and LISP program primitives.␈↓ λ8   .   .   .   .   .   .   .   .   .␈↓    62

␈↓ ↓H␈↓␈↓ α85␈↓ αxRepresenting programs known to terminate.␈↓ π8   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    64

␈↓ ↓H␈↓␈↓ α86␈↓ αxRepresenting programs not known to terminate.␈↓ πx   .   .   .   .   .   .   .   .   .   .   .␈↓    72

␈↓ ↓H␈↓␈↓ α87␈↓ αxRecursively Defined Predicates.␈↓ ε8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    76

␈↓ ↓H␈↓␈↓ α88␈↓ αxAdditional induction principles for proving.␈↓ π8   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    78

␈↓ ↓H␈↓␈↓ α89␈↓ αxPartial functions and the Minimization Schema.␈↓ πx   .   .   .   .   .   .   .   .   .   .   .␈↓    82

␈↓ ↓H␈↓␈↓ α810␈↓ αxTheory of LISP: Algebraic Axioms.␈↓ εx   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    85

␈↓ ↓H␈↓␈↓ α811␈↓ αxExercises␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    88


␈↓ ↓H␈↓IV␈↓ α_PROOFS: EXAMPLES

␈↓ ↓H␈↓␈↓ α81␈↓ αxThe SAMEFRINGE problem.␈↓ ¬x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    90

␈↓ ↓H␈↓␈↓ α82␈↓ αxCorrectness of a program to partition lists.␈↓ π8   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓    94

␈↓ ↓H␈↓␈↓ α83␈↓ αxExercises␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   101


␈↓ ↓H␈↓V␈↓ α_LISP PROGRAMS WITH SIDE EFFECTS

␈↓ ↓H␈↓␈↓ α81␈↓ αxSequential (ALGOL-like) programs.␈↓ εx   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   104

␈↓ ↓H␈↓␈↓ α82␈↓ αxArrays in LISP␈↓ ∧8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   109

␈↓ ↓H␈↓␈↓ α83␈↓ αxDefining macros in LISP␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   112

␈↓ ↓H␈↓␈↓ α84␈↓ αxPseudo-functions that modify list structures.␈↓ π8   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   115
␈↓ ↓H␈↓␈↓ ¬RTable of Contents␈↓ -iii


␈↓ ↓H␈↓␈↓ α85␈↓ αxRe-entrant List Structure.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   118

␈↓ ↓H␈↓␈↓ α86␈↓ αxExercises.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   121


␈↓ ↓H␈↓VI␈↓ α_HOW LISP WORKS

␈↓ ↓H␈↓␈↓ α81␈↓ αxLISP objects.␈↓ ∧8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   124

␈↓ ↓H␈↓␈↓ α82␈↓ αxThe Top level of LISP.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   132

␈↓ ↓H␈↓␈↓ α83␈↓ αxEditing LISP programs.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   138

␈↓ ↓H␈↓␈↓ α84␈↓ αxError Handling.␈↓ ∧x   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   142

␈↓ ↓H␈↓␈↓ α85␈↓ αxDebugging Aids in LISP.␈↓ ¬8   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   143

␈↓ ↓H␈↓␈↓ α86␈↓ αxExercises.␈↓ βx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ 
⎇   147


␈↓ ↓H␈↓BIBLIOGRAPHY


␈↓ ↓H␈↓A␈↓ α_LISP Compilers

␈↓ ↓H␈↓␈↓ α81␈↓ αxLCOM0: listing of MACLISP version.␈↓ εx   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ !   i

␈↓ ↓H␈↓␈↓ α82␈↓ αxLCOM4: listing of MACLISP version.␈↓ εx   .   .   .   .   .   .   .   .   .   .   .   .   .   .␈↓ ⊂   iv


␈↓ ↓H␈↓FUNCTION INDEX
␈↓ ↓H␈↓␈↓ εH␈↓ ?i


␈↓ ↓H␈↓α␈↓ ¬IINTRODUCTION


␈↓ ↓H␈↓        This␈α
book␈α
covers␈α∞recursive␈α
programming␈α
in␈α∞LISP,␈α
computation␈α
with␈α∞symbolic␈α
expressions
␈↓ ↓H␈↓represented␈α∀by␈α∀LISP␈α∀S-expressions,␈α∀representation␈α∀of␈α∀S-expressions␈α∀by␈α∀list␈α∀structure␈α∀in␈α∀the
␈↓ ↓H␈↓memory␈α⊃of␈α⊃a␈α⊃computer,␈α⊃and␈α⊃techniques␈α⊂for␈α⊃mathematically␈α⊃proving␈α⊃that␈α⊃programs␈α⊃meet␈α⊂their
␈↓ ↓H␈↓specifications.  We cover the following main topics.

␈↓ ↓H␈↓1.␈α∩Writing␈α∪recursive␈α∩programs.␈α∪ Most␈α∩students␈α∩will␈α∪have␈α∩already␈α∪learned␈α∩to␈α∪write␈α∩sequential
␈↓ ↓H␈↓programs␈α∂which␈α⊂change␈α∂an␈α⊂initial␈α∂state␈α⊂of␈α∂a␈α⊂computation␈α∂to␈α⊂reach␈α∂a␈α⊂state␈α∂satisfying␈α⊂a␈α∂desired
␈↓ ↓H␈↓condition.␈α Recursive␈αprogramming␈αrequires␈αinventing␈αfunctions␈αthat␈αgive␈αthe␈αanswer␈αin␈αterms␈αof
␈↓ ↓H␈↓the␈α
initial␈αinformation␈α
by␈αbuilding␈α
it␈αup␈α
from␈αalready␈α
available␈αfunctions␈α
and␈αsimpler␈α
cases␈αof␈α
the
␈↓ ↓H␈↓function␈αbeing␈αdefined.␈α Either␈αkind␈αof␈αprogram␈αis␈αuniversal,␈αand␈αthe␈αtwo␈αprogramming␈αstyles␈α
are
␈↓ ↓H␈↓complementary.␈α∪ The␈α∀kind␈α∪of␈α∪recursion␈α∀that␈α∪makes␈α∪good␈α∀programs␈α∪is␈α∀␈↓↓conditional␈α∪expression
␈↓ ↓H␈↓↓recursion␈↓␈α→which␈α_differs␈α→in␈α→important␈α_respects␈α→from␈α→the␈α_recursive␈α→definitions␈α→familiar␈α_to
␈↓ ↓H␈↓mathematicians.

␈↓ ↓H␈↓2.␈α
Computing␈α
with␈α
symbolic␈α
expressions␈α
represented␈α
by␈α
list␈α
structure␈α
in␈α
the␈α
memory␈α
of␈α
a␈α
computer.
␈↓ ↓H␈↓The␈αLISP␈αS-expressions␈αform␈αa␈αdata␈αdomain␈αwith␈αa␈αsimple␈αand␈αregular␈αstructure,␈αand␈αimportant
␈↓ ↓H␈↓computations␈α→with␈α→symbolic␈α→expressions␈α→are␈α→readily␈α→represented␈α→as␈α→functions␈α→defined␈α→by
␈↓ ↓H␈↓conditional␈α∩expression␈α∩recursion.␈α∩ The␈α∩examples␈α∩are␈α∩taken␈α∩from␈α∩tree␈α∩search,␈α∩computing␈α∩with
␈↓ ↓H␈↓algebraic␈αand␈αother␈αexpressions␈αinvolving␈αelementary␈αfunctions,␈αpattern␈αmatching,␈αand␈αcompiling,
␈↓ ↓H␈↓interpreting␈α
and␈α
transforming␈α
recursive␈αprograms.␈α
 Recursive␈α
programming␈α
is␈α
the␈αmain␈α
technique,
␈↓ ↓H␈↓but sequential programs are also discussed.

␈↓ ↓H␈↓        The␈α
student␈α∞who␈α
has␈α∞completed␈α
a␈α∞course␈α
based␈α
on␈α∞this␈α
book␈α∞should␈α
be␈α∞able␈α
to␈α∞use␈α
LISP
␈↓ ↓H␈↓proficiently for symbolic computation and in artificial intelligence applications.

␈↓ ↓H␈↓3.␈α_Proving␈α_programs␈α↔correct.␈α_ A␈α_major␈α↔applied␈α_task␈α_of␈α↔computer␈α_science␈α_is␈α_to␈α↔develop
␈↓ ↓H␈↓mathematical␈α
theory␈α
of␈α
computation␈α
to␈α
the␈αpoint␈α
where␈α
a␈α
program␈α
is␈α
not␈α
considered␈αcomplete␈α
until
␈↓ ↓H␈↓it␈αhas␈α
been␈αproven␈α
to␈αmeet␈α
its␈αspecification␈αand␈α
this␈αproof␈α
has␈αbeen␈α
checked␈αby␈α
a␈αproof-checker
␈↓ ↓H␈↓program.␈α∞ In␈α
particular,␈α∞a␈α
university␈α∞level␈α∞course␈α
in␈α∞programming␈α
for␈α∞computer␈α∞science␈α
students
␈↓ ↓H␈↓should teach them to prove correct the programs they write in the course.

␈↓ ↓H␈↓        While␈αprogram␈α
proving␈αhas␈α
not␈αadvanced␈α
to␈αthe␈α
point␈αwhere␈α
it␈αcan␈α
replace␈αdebugging␈α
most
␈↓ ↓H␈↓programs, it has advanced to the point where it should be studied by all comptter science students.

␈↓ ↓H␈↓        This␈α
book␈α
includes␈α
techniques␈α
for␈αproving␈α
the␈α
extensional␈α
correctness␈α
of␈αrecursive␈α
programs
␈↓ ↓H␈↓in␈αpure␈αLISP,␈αand␈αmost␈αof␈αthe␈αprogramming␈αexercises␈αare␈αwithin␈αeasy␈αrange␈αof␈α
these␈αtechniques.
␈↓ ↓H␈↓Thus␈α∃recent␈α∃Stanford␈α∃examinations␈α∃have␈α∃included␈α∃programming␈α∃problems␈α∃together␈α∃with␈α∃a
␈↓ ↓H␈↓requirement␈α∩that␈α⊃the␈α∩solution␈α⊃be␈α∩proved␈α⊃to␈α∩meet␈α⊃certain␈α∩specifications.␈α⊃ Some␈α∩techniques␈α⊃are
␈↓ ↓H␈↓available␈αfor␈α
non-extensional␈αproperties␈αsuch␈α
as␈αthe␈α
number␈αof␈αoperations␈α
executed,␈αand␈αsome␈α
are
␈↓ ↓H␈↓available for sequential programs.

␈↓ ↓H␈↓        Recursive␈αprograms␈αare␈αrepresented␈αas␈αfunctions␈αin␈αa␈αfirst␈αorder␈αtheory␈αand␈αare␈αdefined␈αby
␈↓ ↓H␈↓a␈α␈↓↓functional␈αequation␈↓␈αwhich␈αis␈αessentially␈αa␈αcopy␈αof␈αthe␈αrecursive␈αdefinition␈αand␈αby␈αa␈α␈↓↓minimization
␈↓ ↓H␈↓↓schema␈↓␈αwhich␈αis␈αa␈αsentence␈αschema␈αof␈αfirst␈αorder␈αlogic␈αwith␈αa␈αfree␈αfunction␈αparameter.␈α When␈αthe
␈↓ ↓H␈↓function␈α
is␈α
total␈αthe␈α
schema␈α
can␈α
be␈αdispensed␈α
with.␈α
 The␈αextensional␈α
properties␈α
proved␈α
are␈αthen
␈↓ ↓H␈↓just␈α∪sentences␈α∪in␈α∪first␈α∀order␈α∪logic,␈α∪and␈α∪its␈α∪familiar␈α∀apparatus␈α∪is␈α∪available␈α∪for␈α∀proofs.␈α∪ The
␈↓ ↓H␈↓technique␈α∩is␈α∩much␈α∩more␈α∩direct␈α∩and␈α∩easier␈α∩to␈α∩use␈α∩than␈α∩the␈α∩methods␈α∩available␈α∩for␈α⊃sequential
␈↓ ↓H␈↓programs such as ␈↓↓inductive assertions␈↓ and the Hoare axiomatization.
␈↓ ↓H␈↓ii␈↓ ¬FINTRODUCTION␈↓ H


␈↓ ↓H␈↓        In␈αfact␈αthe␈αproofs␈αare␈αreadily␈αcomputer-checked␈αby␈αRichard␈αWeyhrauch's␈αfirst␈αorder␈αproof-
␈↓ ↓H␈↓checker␈α∂FOL.␈α∞ However,␈α∂FOL␈α∞has␈α∂many␈α∂conventions␈α∞and␈α∂runs␈α∞rather␈α∂slowly,␈α∞so␈α∂we␈α∂didn't␈α∞feel
␈↓ ↓H␈↓justified␈αin␈αrequiring␈αcomputer␈αchecking␈αof␈αthe␈αproofs␈αin␈αthe␈αcourse␈αon␈αwhich␈αthis␈αbook␈αis␈αbased.
␈↓ ↓H␈↓We␈α∂may␈α∂be␈α⊂able␈α∂to␈α∂include␈α∂computer␈α⊂proof-checking␈α∂in␈α∂a␈α∂future␈α⊂version␈α∂of␈α∂the␈α∂course␈α⊂and␈α∂a
␈↓ ↓H␈↓future edition of the book.

␈↓ ↓H␈↓        Additional␈αtheoretical␈αtopics␈αinclude␈αthe␈αuniversal␈αLISP␈αfunction,␈α␈↓↓eval␈↓␈αwhich␈αalso␈αserves␈αas
␈↓ ↓H␈↓a␈αLISP␈αinterpreter,␈α␈↓↓abstract␈αsyntax␈↓␈αwhich␈αenables␈αconvenient␈αproofs␈αof␈αthe␈αcorrectness␈αof␈αa␈αsimple
␈↓ ↓H␈↓compiler, and a slight discussion of computability.

␈↓ ↓H␈↓        An additional applied topic is syntax directed computation.
␈↓ ↓H␈↓␈↓ εH␈↓ 91


␈↓ ↓H␈↓α␈↓ επChapter I

␈↓ ↓H␈↓α␈↓ ¬εINTRODUCTION TO LISP


␈↓ ↓H␈↓        LISP␈α⊂is␈α⊂a␈α∂language␈α⊂for␈α⊂writing␈α∂programs␈α⊂that␈α⊂do␈α∂symbolic␈α⊂computation.␈α⊂ Information␈α∂is
␈↓ ↓H␈↓coded␈α∩or␈α∩represented␈α∩as␈α∩S-expressions.␈α∩ A␈α∩LISP␈α∩program␈α∩can␈α∩also␈α∩be␈α∩represented␈α∩as␈α∩an␈α∩S-
␈↓ ↓H␈↓expression.␈α∞ This␈α∂gives␈α∞LISP␈α∞the␈α∂special␈α∞ability␈α∞to␈α∂easily␈α∞manipulate␈α∞programs␈α∂as␈α∞well␈α∂as␈α∞other
␈↓ ↓H␈↓sorts␈αof␈αsymbolic␈αdata.␈α In␈α
this␈αchapter␈αwe␈αdescribe␈αnotation␈α
for␈αlists␈αand␈αS-expressions,␈αshow␈α
how
␈↓ ↓H␈↓they␈α∪are␈α∪represented␈α∪in␈α∩a␈α∪computer␈α∪as␈α∪list␈α∪structures,␈α∩and␈α∪describe␈α∪the␈α∪basic␈α∪functions␈α∩and
␈↓ ↓H␈↓predicates␈α∂on␈α⊂the␈α∂domain␈α⊂of␈α∂S-expressions␈α⊂in␈α∂terms␈α⊂of␈α∂the␈α⊂computer␈α∂representation.␈α⊂ We␈α∂then
␈↓ ↓H␈↓describe the basic constructs of LISP and show how they are used to form LISP programs.



␈↓ ↓H␈↓1.  ␈↓αLists.␈↓


␈↓ ↓H␈↓        We␈αbegin␈αwith␈αsome␈αexamples.␈α The␈α
most␈αcommon␈αform␈αof␈αS-expression␈αis␈αthe␈α
list.␈αFigure
␈↓ ↓H␈↓1␈α∞shows␈α∞some␈α∞examples␈α∞of␈α∞lists.␈α∞ The␈α∞list␈α∞(i)␈α∞has␈α∞four␈α∞elements,␈α∞one␈α∞of␈α∞which␈α∞is␈α∂repeated.␈α∞ The
␈↓ ↓H␈↓list␈α(ii)␈αhas␈αfour␈αelements␈αone␈αof␈αwhich␈αis␈αitself␈αa␈αlist.␈α The␈αlist␈α(iii)␈αhas␈αone␈αelement.␈α The␈αlist␈α(iv)
␈↓ ↓H␈↓also has one element which itself is a list.  The list (v) has no elements; it is also written ␈↓¬NIL␈↓.


␈↓ ↓H␈↓␈↓ ¬_(i)␈↓ ε8␈↓¬(A B A E)    ␈↓
␈↓ ↓H␈↓␈↓ ¬_(ii)␈↓ ε8␈↓¬(A B (C D) E)␈↓
␈↓ ↓H␈↓␈↓ ¬_(iii)␈↓ ε8␈↓¬(A)          ␈↓
␈↓ ↓H␈↓␈↓ ¬_(iv)␈↓ ε8␈↓¬((A B C D))  ␈↓
␈↓ ↓H␈↓␈↓ ¬_(v)␈↓ ε8␈↓¬()           ␈↓

␈↓ ↓H␈↓␈↓ ¬∪␈↓αFigure 1.␈↓  Examples of lists.



␈↓ ↓H␈↓        Figure␈α∩2␈α∪shows␈α∩how␈α∩symbolic␈α∪information␈α∩can␈α∩be␈α∪represented␈α∩by␈α∩lists.␈α∪ Each␈α∩example
␈↓ ↓H␈↓consists␈αof␈αa␈α
list␈αand␈αa␈α
"mathematical"␈αrepresentation␈αof␈α
the␈αsame␈αinformation.␈α
 Examples␈α(i)-(iv)
␈↓ ↓H␈↓represent␈α⊂familiar␈α⊂mathematical␈α⊂and␈α⊃logical␈α⊂expressions.␈α⊂ The␈α⊂list␈α⊃(v)␈α⊂is␈α⊂used␈α⊂to␈α⊃represent␈α⊂the
␈↓ ↓H␈↓network␈α∩or␈α∩graph␈α∩shown␈α∩according␈α∩to␈α∩a␈α∩scheme␈α∩whereby␈α∩there␈α∩is␈α∩a␈α∩sublist␈α∩for␈α∩each␈α∩vertex
␈↓ ↓H␈↓consisting of the vertex itself followed by the vertices to which it is connected.

␈↓ ↓H␈↓        The␈αelements␈αof␈αa␈αlist␈αare␈αsurrounded␈αby␈αparentheses␈αand␈αseparated␈αby␈αspaces.␈α A␈α
list␈αmay
␈↓ ↓H␈↓have␈α
any␈αnumber␈α
of␈α
terms␈αand␈α
any␈αof␈α
these␈α
terms␈αmay␈α
themselves␈αbe␈α
lists.␈α
 In␈αthis␈α
case,␈αthe␈α
spaces
␈↓ ↓H␈↓surrounding␈α∞a␈α∞sublist␈α∞may␈α∂be␈α∞omitted,␈α∞and␈α∞extra␈α∞spaces␈α∂between␈α∞elements␈α∞of␈α∞a␈α∞list␈α∂are␈α∞allowed.
␈↓ ↓H␈↓Thus  ␈↓¬(A B(C  D)    E)␈↓  ␈αand  ␈↓¬(A B (C D) E)␈↓  are␈αslightly␈αdifferent␈αways␈αof␈αwriting␈αthe␈αsame
␈↓ ↓H␈↓list.
␈↓ ↓H␈↓2␈↓ εβChapter  I␈↓ H





␈↓ ↓H␈↓␈↓ αX(i)␈↓ βx␈↓¬(PLUS X Y)␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓x + y␈↓

␈↓ ↓H␈↓␈↓ αX(ii)␈↓ βx␈↓¬(PLUS (TIMES X Y) X 3)␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓xy + x + 3␈↓.

␈↓ ↓H␈↓␈↓ αX(iii)␈↓ βx␈↓¬(EXIST X (ALL Y (IMPLIES (P X) (P Y))))␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓∃x: ∀y: [P(x)⊃P(y)]␈↓.

␈↓ ↓H␈↓␈↓ αX(iv)␈↓ βx␈↓¬(INTEGRAL 0 ∞ (TIMES (EXP (TIMES I X Y)) (F X)) X)␈↓
␈↓ ↓H␈↓␈↓ βx␈↓π&␈↓β0␈↓∧␈↓#
∞␈↓#␈↓↓e␈↓∧ixy␈↓↓f(x)dx␈↓.

␈↓ ↓H␈↓␈↓ αX(v)␈↓ βx␈↓¬((A B) (B A C D) (C B D E) (D B C E) (E C D F) (F E))␈↓

␈↓"␈↓ ↓H␈↓␈↓ ε8C
␈↓"␈↓ ↓H␈↓␈↓ ε_≤'~`≥
␈↓"␈↓ ↓H␈↓␈↓ ¬x≤'  ~  `≥
␈↓"␈↓ ↓H␈↓␈↓ ¬8B ≤'    ~    `≥ E
␈↓"␈↓ ↓H␈↓␈↓ ∧(A αααααααα␈↓ ¬H'␈↓ ¬H≥␈↓ ε8~␈↓ π(≤␈↓ π(`αααααααα F
␈↓"␈↓ ↓H␈↓␈↓ ¬X`≥    ~    ≤'
␈↓"␈↓ ↓H␈↓␈↓ ¬x`≥  ~  ≤'
␈↓"␈↓ ↓H␈↓␈↓ ε_`≥~≤'
␈↓"␈↓ ↓H␈↓␈↓ ε8D


␈↓ ↓H␈↓␈↓ ∧5␈↓αFigure 2.␈↓  Information represented as lists.





␈↓ ↓H␈↓2.  ␈↓αAtoms.␈↓


␈↓ ↓H␈↓        The␈α
expressions␈α
␈↓¬A,␈α
B,␈α∞X,␈α
Y,␈α
3,␈α
PLUS␈↓,␈α∞and␈α
␈↓¬ALL␈↓␈α
occurring␈α
in␈α
the␈α∞lists␈α
in␈α
Figures␈α
1␈α∞and␈α
2
␈↓ ↓H␈↓are␈α⊂called␈α⊂atoms.␈α⊂ In␈α⊂general,␈α⊂an␈α⊂atom␈α⊂are␈α∂written␈α⊂as␈α⊂a␈α⊂sequence␈α⊂of␈α⊂capital␈α⊂letters,␈α⊂digits,␈α∂and
␈↓ ↓H␈↓special␈αcharacters␈αwith␈αcertain␈αexclusions.␈α The␈αexclusions␈αare␈α<space>,␈α<carriage␈αreturn>,␈αand␈αthe
␈↓ ↓H␈↓other␈α⊗non-printing␈α⊗characters,␈α∃and␈α⊗also␈α⊗the␈α∃parentheses,␈α⊗brackets,␈α⊗semi-colon,␈α⊗and␈α∃comma.
␈↓ ↓H␈↓Numbers␈α
are␈α∞expressed␈α
as␈α∞signed␈α
decimal␈α∞or␈α
octal␈α∞numbers,␈α
the␈α∞exact␈α
convention␈α∞depending␈α
on
␈↓ ↓H␈↓the␈α↔implementation.␈α↔ Floating␈α↔point␈α↔numbers␈α↔are␈α↔written␈α↔with␈α↔decimal␈α↔points␈α_and,␈α↔when
␈↓ ↓H␈↓appropriate,␈αan␈αexponent␈αnotation␈αdepending␈α
on␈αthe␈αimplementation.␈α The␈αreader␈α
should␈αconsult
␈↓ ↓H␈↓the␈αprogrammer's␈αmanual␈α
for␈αthe␈αLISP␈α
implementation␈αhe␈αintends␈α
to␈αuse.␈α Some␈αfurther␈α
examples
␈↓ ↓H␈↓of␈α⊂atoms␈α⊂are␈α⊂shown␈α⊂in␈α⊂Figure␈α⊂3.␈α⊃ From␈α⊂such␈α⊂atoms␈α⊂we␈α⊂can␈α⊂form␈α⊂lists␈α⊃like␈α⊂␈↓¬((345 3.14159 -
␈↓ ↓H␈↓¬47) A307B THE-LAST-TRUMP -45.21)␈↓.
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ 93



␈↓ ↓H␈↓␈↓ ¬X␈↓¬THE-LAST-TRUMP␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓¬A307B         ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓¬345           ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓¬-47           ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓¬-45.21        ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓¬3.14159       ␈↓

␈↓ ↓H␈↓␈↓ ¬¬␈↓αFigure 3.␈↓  Examples of atoms.





␈↓ ↓H␈↓3.  ␈↓αList structures.␈↓


␈↓ ↓H␈↓        Atoms␈α∞and␈α∞lists␈α∞are␈α∞represented␈α∞in␈α∞the␈α∂memory␈α∞of␈α∞the␈α∞computer␈α∞by␈α∞list␈α∞structures.␈α∂ A␈α∞list
␈↓ ↓H␈↓structure␈α
is␈α
a␈α
collection␈αof␈α
memory␈α
units␈α
called␈α
␈↓↓cons-cells.␈↓␈α  A␈α
cons-cell␈α
generally␈α
consists␈α
of␈αone␈α
or
␈↓ ↓H␈↓possibly␈αtwo␈αconsecutive␈αcomputer␈αwords.␈α It␈αis␈αdivided␈αinto␈αtwo␈αparts,␈αthe␈αa-part␈αand␈αthe␈αd-part,
␈↓ ↓H␈↓with␈αeach␈αpart␈αbeing␈αcapable␈αof␈αcontaining␈αan␈αaddress␈αin␈αmemory.␈αThe␈αlist␈αstructure␈αrepresenting
␈↓ ↓H␈↓a␈αlist␈αcontains␈αa␈αcons-cell␈αfor␈αeach␈αelement␈αof␈αthe␈αlist.␈α The␈αa-part␈αof␈αthe␈αcell␈αcontains␈αthe␈αaddress
␈↓ ↓H␈↓of␈α
the␈α
list␈α
structure␈α
representing␈α
the␈α
element␈α
and␈α
the␈α
d-part␈α
contains␈α
the␈α
address␈α
of␈α
the␈α
cell␈αfor␈α
the
␈↓ ↓H␈↓next␈α∂element␈α⊂of␈α∂the␈α⊂list.␈α∂ These␈α⊂addresses␈α∂are␈α⊂sometimes␈α∂called␈α⊂pointers␈α∂as␈α⊂they␈α∂``point''␈α⊂to␈α∂the
␈↓ ↓H␈↓component list structures.

␈↓ ↓H␈↓A␈α
diagram␈α
shows␈α∞this␈α
more␈α
clearly␈α∞than␈α
words.␈α
 Figure 4␈α∞shows␈α
the␈α
list␈α∞structure␈α
corresponding
␈↓ ↓H␈↓to the list   ␈↓¬(PLUS (TIMES X Y) X 3)␈↓   (which might represent the expression   ␈↓↓xy + x + 3␈↓).
␈↓"
␈↓"
␈↓"␈↓ ↓H␈↓            ⊂αααπααα⊃     ⊂αααπααα⊃     ⊂αααπααα⊃     ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓      ααααα→~   ~   εαααα→~   ~   εαααα→~   ~   εαααα→~   ~   εααααααα⊃
␈↓"␈↓ ↓H␈↓            %απα∀ααα$     %απα∀ααα$     %απα∀ααα$     %απα∀ααα$       ~
␈↓"␈↓ ↓H␈↓              ↓             ~             ~             ↓             ~
␈↓"␈↓ ↓H␈↓             PLUS           ~             ~             3             ~
␈↓"␈↓ ↓H␈↓                            ~  ⊂αααπααα⊃  ~  ⊂αααπααα⊃     ⊂αααπααα⊃  ~
␈↓"␈↓ ↓H␈↓                            %α→~   ~   εααβα→~   ~   εαααα→~   ~   ~  ~
␈↓"␈↓ ↓H␈↓                               %απα∀ααα$  ~  %απα∀ααα$     %απα∀απα$  ~
␈↓"␈↓ ↓H␈↓                                 ↓        ~    ~             ↓   ~    ~
␈↓"␈↓ ↓H␈↓                               TIMES      %απαα$             Y   %ααπα$
␈↓"␈↓ ↓H␈↓                                            ↓                       ↓
␈↓"␈↓ ↓H␈↓                                            X                      NIL
␈↓"
␈↓ ↓H␈↓␈↓ αd␈↓αFigure 4.␈↓  Representation of ␈↓¬(PLUS (TIMES X Y) X 3)␈↓ as a list structure.␈↓¬



␈↓ ↓H␈↓        A␈αprogram␈αrefers␈αto␈αa␈αlist␈αby␈αthe␈αaddress␈αof␈αthe␈αcell␈αfor␈αits␈αfirst␈αelement.␈α According␈αto␈αthis
␈↓ ↓H␈↓convention,␈α
we␈α
see␈α
that␈αthe␈α
a-part␈α
of␈α
this␈αcell␈α
is␈α
the␈α
first␈αelement␈α
of␈α
the␈α
list␈αand␈α
the␈α
d-part␈α
is␈αa
␈↓ ↓H␈↓pointer␈αto␈αa␈αsublist␈αformed␈αby␈αdeleting␈αthe␈αfirst␈αelement.␈α Thus␈αthe␈αfirst␈αword␈αof␈αthe␈αlist␈αstructure
␈↓ ↓H␈↓of␈α
Figure 4␈α
contains␈α
a␈α
pointer␈α
to␈α
the␈α∞list␈α
structure␈α
representing␈α
the␈α
atom␈α
␈↓¬PLUS␈↓,␈α
 while␈α∞its␈α
d-part
␈↓ ↓H␈↓points␈α
to␈α
the␈α
list␈α
␈↓¬((TIMES X Y) X 3)␈↓.␈α
 The␈α
second␈α
word␈α
contains␈α
the␈α
list␈α∞structure␈α
representing
␈↓ ↓H␈↓␈↓¬(TIMES X Y)␈↓␈αin␈αits␈αa-part␈αand␈αthe␈αlist␈αstructure␈αrepresenting␈α␈↓¬(X 3)␈↓␈αin␈αits␈αd-part.␈α The␈αlast␈αword
␈↓ ↓H␈↓4␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓points␈α⊂to␈α∂the␈α⊂atom␈α∂␈↓¬3␈↓␈α⊂in␈α∂its␈α⊂a-part␈α∂and␈α⊂has␈α⊂a␈α∂pointer␈α⊂to␈α∂the␈α⊂atom␈α∂␈↓¬NIL␈↓␈α⊂in␈α∂its␈α⊂d-part.␈α⊂ This␈α∂is
␈↓ ↓H␈↓consistent with the convention that ␈↓¬NIL␈↓ represents the null list.

␈↓ ↓H␈↓        Atoms␈αare␈αrepresented␈αby␈αthe␈αaddresses␈αof␈αtheir␈αproperty␈αlists␈αwhich␈αare␈αlist␈αstructures␈αof␈αa
␈↓ ↓H␈↓special␈α∂kind␈α⊂depending␈α∂on␈α∂the␈α⊂implementation.␈α∂ (In␈α⊂some␈α∂implementations,␈α∂the␈α⊂first␈α∂word␈α⊂of␈α∂a
␈↓ ↓H␈↓property␈α
list␈α
is␈α
in␈α
a␈α
special␈αarea␈α
of␈α
memory,␈α
in␈α
others␈α
the␈αfirst␈α
word␈α
is␈α
distinguished␈α
by␈α
sign,␈αin
␈↓ ↓H␈↓still␈αothers␈αit␈αhas␈αa␈αspecial␈αa-part.␈α For␈αbasic␈αLISP␈αprogramming,␈αit␈αis␈αenough␈αto␈αknow␈αthat␈αatoms
␈↓ ↓H␈↓are␈α
distinguishable␈α
from␈α
other␈α
list␈α
structures␈α∞by␈α
a␈α
predicate␈α
called␈α
␈↓αat␈↓.)␈α
Each␈α
atom␈α∞is␈α
represented
␈↓ ↓H␈↓uniquely.  In the diagram they are simply refered to by name.

␈↓ ↓H␈↓A␈αpartial␈αdump␈αof␈αthe␈αmemory␈αof␈αa␈αcomputer␈αcontaining␈αthe␈αlist␈αstructure␈αof␈αFigure 4␈αmight␈αlook
␈↓ ↓H␈↓as␈αshown␈αin␈αFigure 5.␈α Here␈α
␈↓X␈↓␈αdenotes␈αthe␈αaddress␈αof␈αthe␈α
property␈αlist␈αof␈αthe␈αatom␈αnamed␈α
␈↓¬X␈α␈↓and
␈↓ ↓H␈↓␈↓/3␈↓␈αdenotes␈αthat␈αof␈αthe␈αatom␈αnamed␈α␈↓¬3␈↓.␈α Notice␈αthat␈αthe␈αaddresses␈αof␈αconsecutive␈αlist␈αelements␈αneed
␈↓ ↓H␈↓not␈αbe␈αconsecutive.␈α Also␈αthe␈αlast␈αword␈αof␈αa␈αlist␈αcannot␈αhave␈αthe␈αaddress␈αof␈αa␈αnext␈αword␈αin␈αits␈αd-
␈↓ ↓H␈↓part since there isn't any next word, so it has the address of a special atom called ␈↓¬NIL␈↓.

␈↓ ↓H␈↓        address    a-part    d-part        address    a-part    d-part
␈↓ ↓H␈↓        _______    ______    ______        _______    ______    ______

␈↓ ↓H␈↓            86      TIMES       87           1000      PLUS      1001
␈↓ ↓H␈↓            87          X       88           1001        86      1002
␈↓ ↓H␈↓            88          Y      NIL           1002         X      2653
␈↓ ↓H␈↓                                             2653        /3       NIL

␈↓ ↓H␈↓␈↓ βG␈↓αFigure 5.␈↓  A memory map for the list structure of Figure 4.





␈↓ ↓H␈↓4.  ␈↓αS-expressions.␈↓


␈↓ ↓H␈↓        When␈α∪we␈α∪examine␈α∩the␈α∪way␈α∪list␈α∩structures␈α∪represent␈α∪lists␈α∩we␈α∪see␈α∪a␈α∪curious␈α∩asymmetry.
␈↓ ↓H␈↓Namely,␈αthe␈αa-part␈αof␈αa␈αlist␈αword␈αcan␈αcontain␈αan␈αatom␈αor␈αa␈αlist,␈αbut␈αthe␈αd-part␈αcan␈αcontain␈αonly␈α
a
␈↓ ↓H␈↓list␈α
or␈αthe␈α
special␈αatom␈α
␈↓¬NIL␈↓.␈α This␈α
restriction␈α
is␈αquite␈α
unnatural␈αfrom␈α
the␈αcomputing␈α
point␈αof␈α
view,
␈↓ ↓H␈↓and␈αwe␈αshall␈α
allow␈αarbitrary␈αatoms␈α
to␈αinhabit␈αthe␈α
d-parts␈αof␈αwords,␈α
but␈αthen␈αwe␈α
must␈αgeneralize
␈↓ ↓H␈↓the␈α
way␈α
list␈αstructures␈α
are␈α
expressed␈αas␈α
character␈α
strings.␈α
To␈αdo␈α
this,␈α
we␈αintroduce␈α
the␈α
notion␈αof␈α
S-
␈↓ ↓H␈↓expression.

␈↓ ↓H␈↓        An␈α
S-expression␈α
is␈α
either␈α
an␈α
atom␈α
or␈α
a␈α
pair␈α
of␈α
S-expressions.␈α
 To␈α
write␈α
a␈α
non-atomic␈α
S-
␈↓ ↓H␈↓expression␈α
we␈αwrite␈α
the␈α
pair␈αof␈α
subexpression␈αseparated␈α
by␈α
" . "␈αand␈α
surrounded␈α
by␈αparentheses.
␈↓ ↓H␈↓In BNF this rule is given by:

␈↓ ↓H␈↓␈↓ β	        <S-expression> ::= <atom> | (<S-expression> . <S-expression>).

␈↓ ↓H␈↓Figure 6 shows some examples of S-expressions.
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ 95



␈↓ ↓H␈↓␈↓ ¬␈↓¬A                        ␈↓
␈↓ ↓H␈↓␈↓ ¬␈↓¬(A . B)                  ␈↓
␈↓ ↓H␈↓␈↓ ¬␈↓¬(A . (B . A))            ␈↓
␈↓ ↓H␈↓␈↓ ¬␈↓¬(PLUS . (X . (Y . NIL))) ␈↓
␈↓ ↓H␈↓␈↓ ¬␈↓¬(3 . 3.4)                ␈↓

␈↓ ↓H␈↓␈↓ ∧R␈↓αFigure 6.␈↓  Examples of S-expressions.



␈↓ ↓H␈↓In␈α
writing␈α
an␈α
S-expression,␈α
the␈α
spaces␈α
around␈α
the␈α" . "␈α
may␈α
be␈α
omitted␈α
when␈α
this␈α
will␈α
not␈αcause
␈↓ ↓H␈↓confusion.␈α The␈αonly␈αpossible␈αconfusion␈αis␈αof␈αthe␈αdot␈αseparator␈αwith␈αa␈αdecimal␈αpoint␈α
in␈αnumbers.
␈↓ ↓H␈↓Thus,␈αin␈αthe␈αabove␈αcases,␈αwe␈αmay␈αwrite␈α␈↓¬(A.B)␈↓,␈α␈↓¬(A.(B.A))␈↓,␈αand␈α␈↓¬(PLUS.(X.(Y.NIL)))␈↓,␈αbut␈αif␈αwe
␈↓ ↓H␈↓wrote ␈↓¬(3.3.4)␈↓ confusion would result.

␈↓ ↓H␈↓        In␈αthe␈αmemory␈αof␈αa␈αcomputer,␈αan␈αS-expression␈αis␈αrepresented␈αby␈αthe␈αaddress␈αof␈α
a␈αcons-cell
␈↓ ↓H␈↓whose␈α
a-part␈α
points␈αto␈α
the␈α
first␈αelement␈α
of␈α
the␈α
pair␈αand␈α
whose␈α
d-part␈αpoints␈α
to␈α
the␈αsecond␈α
element
␈↓ ↓H␈↓of␈α_the␈α_pair.␈α_ Thus,␈α_the␈α_S-expressions␈α_␈↓¬(A.B)␈↓,␈α_␈↓¬(A.(B.A))␈↓,␈α_and␈α_␈↓¬(PLUS.(X.(Y.NIL)))␈↓␈α_are
␈↓ ↓H␈↓represented by the list structures of Figure 7.
␈↓"
␈↓"
␈↓"␈↓ ↓H␈↓            ⊂αααπααα⊃                             ⊂αααπααα⊃     ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓       αααα→~   ~   ~                        αααα→~   ~   εαααα→~   ~   ~
␈↓"␈↓ ↓H␈↓            %απα∀απα$                             %απα∀ααα$     %απα∀απα$
␈↓"␈↓ ↓H␈↓              ↓   ↓                                 ~             ↓   ~
␈↓"␈↓ ↓H␈↓              A   B                                 ~             B   ~
␈↓"␈↓ ↓H␈↓                                                    ~                 ~
␈↓"␈↓ ↓H␈↓                                                    ~                 ~
␈↓"␈↓ ↓H␈↓                                                    %ααααααααπαααααααα$
␈↓"␈↓ ↓H␈↓                                                             ↓
␈↓"␈↓ ↓H␈↓                                                             A
␈↓"
␈↓"␈↓ ↓H␈↓            ⊂αααπααα⊃     ⊂αααπααα⊃     ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓       αααα→~   ~   εαααα→~   ~   εαααα→~   ~   ~
␈↓"␈↓ ↓H␈↓            %απα∀ααα$     %απα∀ααα$     %απα∀απα$
␈↓"␈↓ ↓H␈↓              ↓             ↓             ↓   ↓
␈↓"␈↓ ↓H␈↓             PLUS           X             Y  NIL
␈↓"
␈↓"
␈↓ ↓H␈↓␈↓ βD␈↓αFigure 7.␈↓  Representation of S-expressions as list structures.



␈↓ ↓H␈↓        Note␈α∩that␈α∩the␈α∩list␈α∩␈↓¬(PLUS X Y)␈↓␈α∩and␈α∩the␈α∩S-expression␈α∩ ␈↓¬(PLUS . (X . (Y . NIL)))␈↓␈α∩are
␈↓ ↓H␈↓represented␈αin␈α
memory␈αby␈α
the␈αsame␈α
list␈αstructure.␈α
 The␈αsimplest␈α
way␈αto␈α
treat␈αthis␈α
is␈αto␈α
regard␈αS-
␈↓ ↓H␈↓expressions␈α
as␈αprimary␈α
and␈αlists␈α
as␈αspecial␈α
kinds␈αof␈α
S-expressions,␈αnamely␈α
those␈αthat␈α
never␈αhave
␈↓ ↓H␈↓any␈α
atom␈α
but␈α
␈↓¬NIL␈↓␈α
as␈α
the␈αsecond␈α
part␈α
of␈α
a␈α
pair.␈α
 The␈αlist␈α
notation␈α
can␈α
then␈α
be␈α
considered␈α
as␈αan
␈↓ ↓H␈↓abbreviated␈α∞way␈α
of␈α∞writing␈α
these␈α∞S-expressions,␈α
Thus,␈α∞the␈α
list␈α∞notation␈α
␈↓¬(A B . . . Z)␈↓␈α∞may␈α
be
␈↓ ↓H␈↓regarded␈α?␈α↔as␈α?␈α_an␈α?␈α↔abbreviation␈α?␈α_of␈α?␈α↔the␈α?␈α_S-expression␈α?␈α↔notation
␈↓ ↓H␈↓␈↓¬(A . (B . (C . (... (Z . NIL) ...))))␈↓.␈α∪ Note␈α∪that␈α∪the␈α∪a-part␈α∪of␈α∪a␈α∪list␈α∪can␈α∪be␈α∪any␈α∪S-
␈↓ ↓H␈↓expression,␈αbut␈αthe␈αd-part␈α
of␈αa␈αlist␈αmust␈αbe␈α
a␈αlist␈αand␈αand␈αthe␈α
only␈αatomic␈αlist␈αis␈α␈↓¬NIL␈↓.␈α
 In␈αgiving
␈↓ ↓H␈↓input␈α
to␈α∞LISP,␈α
either␈α∞the␈α
list␈α∞form␈α
or␈α∞the␈α
S-expression␈α∞form␈α
may␈α∞be␈α
used␈α∞for␈α
lists.␈α∞ On␈α
output,
␈↓ ↓H␈↓LISP␈αwill␈αprint␈α
a␈αlist␈αstructure␈α
as␈αa␈αlist␈α
as␈αfar␈αas␈α
it␈αcan,␈αotherwise␈α
as␈αan␈αS-expression.␈α Thus,␈α
some
␈↓ ↓H␈↓list␈α
structures␈αwill␈α
be␈αprinted␈α
in␈αa␈α
mixed␈αnotation,␈α
e.g.␈α␈↓¬((A . B) (C . D) (3))␈↓.␈α
 Some␈αLISPs␈α
use
␈↓ ↓H␈↓6␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓an␈α
"extended"␈αlist␈α
notation␈αfor␈α
printing␈α
S-expressions␈αwhich␈α
minimizes␈αthe␈α
number␈αof␈α
parentheses
␈↓ ↓H␈↓and␈α⊗dots␈α↔printed.␈α⊗  In␈α↔this␈α⊗notation␈α⊗␈↓¬(A . (B . (C . D)))␈↓␈α↔would␈α⊗print␈α↔as␈α⊗␈↓¬(A B C . D)␈↓.
␈↓ ↓H␈↓(Programs for reading and printing S-expressions in the two notations are given in Chapter VI.)


␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓        1.␈α≠If␈α≠we␈α≠represent␈α≠sums␈α≠and␈α≠products␈α≠as␈α≠indicated␈α≠above␈α≠and␈α≠use␈α~␈↓¬(MINUS X)␈↓,
␈↓ ↓H␈↓␈↓¬(QUOTIENT X Y)␈↓, and ␈↓¬(POWER X Y)␈↓ as representations of ␈↓↓-x,␈↓ ␈↓↓x/y,␈↓ and ␈↓↓x␈↓∧y␈↓ respectively, then

␈↓ ↓H␈↓                a. What do the following lists represent?

␈↓ ↓H␈↓␈↓ ∧␈↓↓␈↓¬(QUOTIENT 2 (POWER (PLUS X (MINUS Y)) 3))␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βp␈↓↓␈↓¬(PLUS -2 (MINUS 2) (TIMES X (POWER Y 3.3)))␈↓↓␈↓

␈↓ ↓H␈↓                b. How are the expressions ␈↓↓xyz+3(u+v)␈↓∧-3␈↓ and ␈↓↓(xy-yx)/(xy+yx)␈↓ to be represented?

␈↓ ↓H␈↓        2. In the above mentioned graph notation, what graph is represented by the list

␈↓ ↓H␈↓␈↓ αZ␈↓¬((A D E F) (B D E F) (C D E F) (D A B C) (E A B C) (F A B C))␈↓?

␈↓ ↓H␈↓        3.␈α
Write␈α
the␈αlist␈α
␈↓¬(PLUS␈α
(TIMES␈αX␈α
Y)␈α
X␈α3)␈↓␈α
as␈α
an␈αS-expression.␈α
 This␈α
is␈α
sometimes␈αreferred
␈↓ ↓H␈↓to as "dot-notation".

␈↓ ↓H␈↓        4.  Write the following S-expressions in list notation to whatever extent is possible:

␈↓ ↓H␈↓                a. ␈↓¬(A . NIL)␈↓
␈↓ ↓H␈↓                b. ␈↓¬(A . B)␈↓
␈↓ ↓H␈↓                c. ␈↓¬((A . NIL) . B)␈↓
␈↓ ↓H␈↓                d. ␈↓¬((A . B) . ((C . D) . NIL))␈↓.

␈↓ ↓H␈↓        5.␈α
How␈α
would␈α
you␈α
represent␈α
polynomials␈α
in␈αone␈α
variable␈α
as␈α
lists?␈α
 Can␈α
you␈α
think␈α
of␈αmore
␈↓ ↓H␈↓that␈α
one␈α∞way?␈α
 How␈α∞would␈α
you␈α∞represent␈α
polynomials␈α∞in␈α
several␈α∞variables?␈α
 Can␈α∞you␈α
tell␈α∞if␈α
two
␈↓ ↓H␈↓lists represent the same polynomial?

␈↓ ↓H␈↓        6.␈αProve␈αthat␈αthe␈αnumber␈αof␈α"shapes"␈αof␈αS-expressions␈αwith␈α␈↓↓n␈↓␈αatoms␈αis␈α␈↓↓(2n␈α-␈α2)!/(n!(n␈α-␈α1)!)␈↓.
␈↓ ↓H␈↓(The two shapes of S-expressions with three atoms are ␈↓¬(A.(B.C))␈↓ and ␈↓¬((A.B).C)␈↓.

␈↓ ↓H␈↓        7.␈αThe␈αabove␈αresult␈αcan␈αalso␈αbe␈αobtained␈αby␈αwriting␈α␈↓↓S = A + S␈↓π#␈↓↓S␈↓␈αas␈αan␈α"equation"␈αsatisfied
␈↓ ↓H␈↓by␈α∞the␈α∞set␈α∞of␈α
S-expressions␈α∞with␈α∞the␈α∞interpretation␈α
that␈α∞an␈α∞S-expression␈α∞is␈α
either␈α∞an␈α∞atom␈α∞or␈α
a
␈↓ ↓H␈↓pair␈αof␈α
S-expressions.␈α The␈αnext␈α
step␈αis␈α
to␈αregard␈αthe␈α
equation␈αas␈αthe␈α
quadratic␈α␈↓↓S␈↓∧2␈↓↓ - S + A␈α
=␈α0␈↓,
␈↓ ↓H␈↓solve␈α
it␈α
by␈α
the␈α
quadratic␈α
formula␈α
choosing␈α
the␈α
minus␈α
sign␈α
for␈α
the␈α
square␈α
root.␈α
 Then␈α
the␈αradical␈α
is
␈↓ ↓H␈↓regarded␈α
as␈α
the␈α
1/2␈α
power␈α
and␈α∞expanded␈α
by␈α
the␈α
binomial␈α
theorem.␈α
 Manipulating␈α∞the␈α
binomial
␈↓ ↓H␈↓coefficients␈α∞yields␈α∞the␈α∂above␈α∞formula␈α∞as␈α∞the␈α∂coefficient␈α∞of␈α∞␈↓↓A␈↓∧n␈↓␈α∞in␈α∂the␈α∞expansion.␈α∞ Why␈α∂does␈α∞this
␈↓ ↓H␈↓somewhat␈α
ill-motivated␈α
and␈αirregular␈α
procedure␈α
give␈αthe␈α
right␈α
answer?␈α The␈α
numbers␈α
are␈αcalled
␈↓ ↓H␈↓Catalan numbers.

␈↓ ↓H␈↓        (The last two exercises are unconnected with subsequent material in this book.)
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ 97


␈↓ ↓H␈↓5.  ␈↓αThe basic functions and predicates on S-expressions.␈↓


␈↓ ↓H␈↓        There␈α⊂are␈α⊂three␈α⊂basic␈α⊂functions␈α⊂on␈α⊂S-expressions:␈α⊂␈↓↓cons␈↓␈α⊂for␈α⊂constructing␈α⊂an␈α⊂S-expression
␈↓ ↓H␈↓from␈αtwo␈αgiven␈αS-expressions,␈αand␈α␈↓↓car␈↓␈αand␈α␈↓↓cdr␈↓␈αfor␈αselecting␈αthe␈αfirst␈αand␈αsecond␈αcomponents␈αof␈αa
␈↓ ↓H␈↓non-atomic␈α∩S-expression.␈α∩ There␈α∪are␈α∩two␈α∩predicates,␈α∪␈↓↓atom␈↓␈α∩for␈α∩testing␈α∪whether␈α∩or␈α∩not␈α∪an␈α∩S-
␈↓ ↓H␈↓expression␈αis␈α
atomic␈αand␈α
␈↓↓eq␈↓␈αfor␈α
testing␈αequality␈α
of␈αatoms.␈α
 In␈αthis␈α
section␈αwe␈α
will␈αdescribe␈αthe␈α
basic
␈↓ ↓H␈↓LISP␈αprograms␈αthat␈αcompute␈αthese␈αfunctions␈αand␈αpredicates␈αfrom␈αlist␈αstructure␈αrepresentations␈αof
␈↓ ↓H␈↓the␈αarguments.␈α All␈αLISP␈αprograms␈αfor␈αcomputing␈αfunctions␈αand␈αpredicates␈αon␈α
S-expressions␈αare
␈↓ ↓H␈↓built␈α⊃from␈α⊃these␈α⊃basic␈α⊃programs␈α⊃as␈α⊃will␈α⊃be␈α⊃explained␈α⊃in␈α⊃the␈α⊃sections␈α⊃following␈α⊃this.␈α⊃ This␈α⊃is
␈↓ ↓H␈↓analogous␈α
to␈αnumeric␈α
programs␈α
which␈αare␈α
based␈α
on␈αoperations␈α
computing␈α
the␈αarithmetic␈α
functions
␈↓ ↓H␈↓of␈αaddition␈αsubtraction,␈αmultiplication,␈αand␈αdivision,␈α
and␈αthe␈αarithmetic␈αpredicates␈αof␈αequality␈α
and
␈↓ ↓H␈↓comparison.

␈↓ ↓H␈↓        First␈α
we␈α∞introduce␈α
the␈α∞notation␈α
to␈α∞be␈α
used␈α∞for␈α
writing␈α∞LISP␈α
terms.␈α∞ This␈α
notation␈α∞will␈α
be
␈↓ ↓H␈↓extended␈α⊃in␈α⊂later␈α⊃sections␈α⊂to␈α⊃express␈α⊂LISP␈α⊃programs␈α⊂as␈α⊃well.␈α⊂ A␈α⊃LISP␈α⊂term␈α⊃is␈α⊃an␈α⊂expression
␈↓ ↓H␈↓intended␈α
to␈α
denote␈α
an␈α
S-expression␈α
(the␈α
value␈αof␈α
the␈α
term).␈α
 We␈α
will␈α
use␈α
two␈α
languages␈αfor␈α
writing
␈↓ ↓H␈↓LISP␈α⊗terms␈α⊗and␈α⊗programs␈α⊗-␈α∃␈↓↓internal␈α⊗language␈↓␈α⊗and␈α⊗␈↓↓external␈α⊗language␈↓.␈α⊗ Internal␈α∃language
␈↓ ↓H␈↓represents␈αLISP␈αterms␈αand␈αprograms␈αas␈αS-expressions.␈α Internal␈αlanguage␈αprograms␈αare␈αsomewhat
␈↓ ↓H␈↓harder␈α⊃for␈α⊃a␈α⊃person␈α⊃to␈α⊃read␈α∩and␈α⊃write,␈α⊃but␈α⊃it␈α⊃is␈α⊃easier␈α∩for␈α⊃a␈α⊃person␈α⊃to␈α⊃write␈α⊃a␈α∩program␈α⊃to
␈↓ ↓H␈↓manipulate␈α␈↓↓object␈↓␈α␈↓↓programs␈↓␈αwhen␈αthe␈αobject␈αprograms␈αare␈αin␈αinternal␈αlanguage␈α(e.g.␈αrepresented␈α
as
␈↓ ↓H␈↓S-expressions).␈α External␈αlanguage␈αis␈αa␈αnotation␈αthat␈α
is␈αcompact␈αand␈αeasier␈αfor␈αpeople␈αto␈αread␈α
and
␈↓ ↓H␈↓write.␈α However,␈αexternal␈αlanguage␈αprograms␈αare␈αnot␈αS-expressions,␈αand␈αtherefore␈αit␈αis␈αnot␈αeasy␈α
to
␈↓ ↓H␈↓write␈αLISP␈αprograms␈αto␈αgenerate,␈α
interpret,␈αcompile␈αor␈αotherwise␈αmanipulate␈αprograms␈α
written␈αin
␈↓ ↓H␈↓external␈α
language.␈α
 (An␈α
additional␈α
advantage␈α
of␈α
the␈α
external␈α
notation␈α
is␈α
that␈α
it␈α
very␈α
similar␈α
the
␈↓ ↓H␈↓language used to express facts about the abstract domain of S-expressions given in Chapter III).

␈↓ ↓H␈↓[Remark:␈α∞most␈α∞LISP␈α∞implementations␈α∞accept␈α∂only␈α∞internal␈α∞language␈α∞programs␈α∞rather␈α∂than␈α∞some
␈↓ ↓H␈↓form of external language.]

␈↓ ↓H␈↓        The␈α
simplest␈αLISP␈α
terms␈αare␈α
variables␈αand␈α
constants.␈α In␈α
the␈αexternal␈α
language␈αthe␈α
notation
␈↓ ↓H␈↓for␈α∂S-expression␈α∞constants␈α∂is␈α∞that␈α∂described␈α∞in␈α∂the␈α∞preceeding␈α∂sections.␈α∂ (S-expression␈α∞constants
␈↓ ↓H␈↓will␈α
always␈α
appear␈α
in␈α
the␈αspecial␈α
font␈α
used␈α
in␈α
the␈αprevious␈α
sections.)␈α
In␈α
the␈α
internal␈αlanguage␈α
there
␈↓ ↓H␈↓is␈α∞a␈α
possible␈α∞ambiguity␈α∞as␈α
to␈α∞whether␈α∞an␈α
S-expression␈α∞is␈α∞to␈α
represent␈α∞itself,␈α∞or␈α
the␈α∞value␈α∞of␈α
the
␈↓ ↓H␈↓LISP␈α⊂term␈α⊂that␈α⊂it␈α⊃represents␈α⊂(if␈α⊂any).␈α⊂ LISP␈α⊂takes␈α⊃the␈α⊂latter␈α⊂point␈α⊂of␈α⊂view.␈α⊃ An␈α⊂S-expression
␈↓ ↓H␈↓constant␈αis␈αrepresented␈αby␈αa␈αlist␈αwhose␈αfirst␈αelement␈αis␈αthe␈αatom␈α␈↓¬QOUTE␈α␈↓and␈αwhose␈αsecond␈αelement
␈↓ ↓H␈↓is␈α⊃the␈α⊃S-expression.␈α⊃ Thus␈α⊃the␈α∩S-expression␈α⊃constant␈α⊃␈↓¬<e>␈↓␈α⊃is␈α⊃represented␈α⊃by␈α∩the␈α⊃S-expression
␈↓ ↓H␈↓␈↓¬(QUOTE <e>)␈↓.

␈↓ ↓H␈↓        Variables␈α∞are␈α∞written␈α∞in␈α∞external␈α∞notation␈α∞as␈α∞lower␈α∞case␈α∞italic␈α∞identifiers.␈α∂ Frequently␈α∞just
␈↓ ↓H␈↓single␈αletters␈αsuch␈αas␈α␈↓↓x␈↓␈αor␈α␈↓↓u␈↓␈αare␈αused,␈α but␈αoccasionly␈αwe␈αuse␈αa␈αname␈αintended␈αto␈αsuggest␈αa␈αspecial
␈↓ ↓H␈↓subset␈α
of␈α
the␈α
S-expressions␈αas␈α
the␈α
intended␈α
range.␈α
 The␈αcorresponding␈α
internal␈α
form␈α
will␈α
be␈αthe
␈↓ ↓H␈↓atom␈α∞whose␈α∞name␈α∞is␈α∞the␈α∞same␈α∞identifier␈α∂but␈α∞in␈α∞upper␈α∞case␈α∞(and␈α∞appearing␈α∞in␈α∂the␈α∞S-expression
␈↓ ↓H␈↓font).␈α⊃ Thus␈α⊃␈↓¬X␈α⊃␈↓corresponds␈α⊃to␈α⊃␈↓↓x␈↓␈α⊃and␈α⊃␈↓¬U␈α⊃␈↓to␈α⊃␈↓↓u.␈↓␈α⊃ We␈α⊃will␈α⊃generally␈α⊃use␈α⊃␈↓↓x,␈↓␈α⊃␈↓↓y,␈↓␈α⊃␈↓↓z␈↓␈α⊃to␈α∩range␈α⊃over
␈↓ ↓H␈↓arbitrary S-expressions and ␈↓↓u,␈↓ ␈↓↓v,␈↓ ␈↓↓w␈↓ to range over lists.

␈↓ ↓H␈↓        Additional␈αterms␈α(called␈αapplication␈αterms)␈αcan␈αbe␈αformed␈αdenoting␈αthe␈αresult␈αof␈αone␈αof␈αthe
␈↓ ↓H␈↓basic␈α⊃LISP␈α⊂programs.␈α⊃These␈α⊃are␈α⊂expressed␈α⊃in␈α⊃external␈α⊂language␈α⊃using␈α⊃ordinary␈α⊂mathematical
␈↓ ↓H␈↓notation␈α∩for␈α⊃the␈α∩application␈α∩of␈α⊃functions␈α∩and␈α⊃predicates␈α∩to␈α∩a␈α⊃suitable␈α∩number␈α∩of␈α⊃arguments.
␈↓ ↓H␈↓Program␈α∀names␈α∀are␈α∃used␈α∀for␈α∀function␈α∀symbols␈α∃and␈α∀variables␈α∀or␈α∀S-expression␈α∃constants␈α∀as
␈↓ ↓H␈↓8␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓arguments.␈α⊗ Argument␈α⊗lists␈α⊗will␈α⊗be␈α⊗surrounded␈α⊗by␈α⊗``[]''␈α⊗thus␈α⊗reserving␈α⊗parentheses␈α⊗for␈α∃S-
␈↓ ↓H␈↓expressions.␈α
 The␈αbrackets␈α
will␈α
often␈αbe␈α
omitted␈αfor␈α
single␈α
arguments.␈α The␈α
internal␈α
notation␈αfor
␈↓ ↓H␈↓application␈α⊂terms␈α∂is␈α⊂more␈α∂uniform.␈α⊂ Namely␈α⊂such␈α∂an␈α⊂application␈α∂term␈α⊂is␈α∂represented␈α⊂by␈α⊂a␈α∂list,
␈↓ ↓H␈↓where␈α∩the␈α⊃first␈α∩element␈α⊃is␈α∩the␈α∩atom␈α⊃corresponding␈α∩to␈α⊃the␈α∩program␈α⊃name␈α∩and␈α∩the␈α⊃remaining
␈↓ ↓H␈↓elements of the list represent the arguments.

␈↓ ↓H␈↓        Table␈α∀1.␈α∪gives␈α∀some␈α∪examples␈α∀of␈α∀LISP␈α∪terms.␈α∀ Each␈α∪term␈α∀is␈α∪shown␈α∀in␈α∀external␈α∪and
␈↓ ↓H␈↓internal␈α
notation␈α
along␈α
with␈α
the␈α
S-expression␈α
value␈α
that␈α
it␈α
denotes.␈α
 Note␈α
that␈α
no␈αvariables␈α
appear
␈↓ ↓H␈↓in␈α
these␈α
examples␈α
because␈α∞the␈α
value␈α
denoted␈α
by␈α
a␈α∞LISP␈α
term␈α
containing␈α
a␈α
variable␈α∞depends␈α
on
␈↓ ↓H␈↓the value assigned to the variable.

␈↓ ↓H␈↓␈↓ α8External form␈↓ ∧xInternal form␈↓ 	xValue denoted

␈↓ ↓H␈↓␈↓ ↓H(i)␈↓ α8␈↓¬(A . B)␈↓␈↓ ∧x␈↓¬(QUOTE (A . B))␈↓␈↓ 	x␈↓¬(A . B)␈↓
␈↓ ↓H␈↓␈↓ α8␈↓¬(CAR  X)␈↓␈↓ ∧x␈↓¬(QUOTE (CAR  X))␈↓␈↓ 	x␈↓¬(CAR X)␈↓

␈↓ ↓H␈↓␈↓ ↓H(iia)␈↓ α8␈↓αa|␈↓␈↓¬(A . B)␈↓␈↓ ∧x␈↓¬(CAR (QUOTE (A . B)))␈↓␈↓ 	x␈↓¬A␈↓
␈↓ ↓H␈↓␈↓ ↓H(iid)␈↓ α8␈↓αd|␈↓␈↓¬(A . B)␈↓␈↓ ∧x␈↓¬(CDR (QUOTE (A . B)))␈↓␈↓ 	x␈↓¬B␈↓
␈↓ ↓H␈↓␈↓ ↓H(iic)␈↓ α8␈↓↓cons[␈↓¬A,B␈↓↓]␈↓␈↓ ∧x␈↓¬(CONS (QUOTE A) (QUOTE B))␈↓␈↓ 	x␈↓¬(A . B)␈↓

␈↓ ↓H␈↓␈↓ ↓H(iiia)␈↓ α8␈↓αa|␈↓␈↓¬((A . B) . A)␈↓␈↓ ∧x␈↓¬(CAR (QUOTE ((A . B) . A))␈↓␈↓ 	x␈↓¬(A . B)␈↓
␈↓ ↓H␈↓␈↓ ↓H(iiid)␈↓ α8␈↓αd|␈↓␈↓¬((A . B) . A)␈↓␈↓ ∧x␈↓¬(CDR (QUOTE ((A . B) . A))␈↓␈↓ 	x␈↓¬A␈↓
␈↓ ↓H␈↓␈↓ ↓H(iiic)␈↓ α8␈↓↓cons[␈↓¬(A . B),A␈↓↓]␈↓␈↓ ∧x␈↓¬(CONS (QUOTE (A . B)) (QUOTE A))␈↓␈↓ 	x␈↓¬((A . B) . A)␈↓

␈↓ ↓H␈↓␈↓ ↓H(iva)␈↓ α8␈↓αa|␈↓␈↓¬(A B C D)␈↓␈↓ ∧x␈↓¬(CAR (QUOTE (A B C D)))␈↓␈↓ 	x␈↓¬A␈↓
␈↓ ↓H␈↓␈↓ ↓H(ivd)␈↓ α8␈↓αd|␈↓␈↓¬(A B C D)␈↓␈↓ ∧x␈↓¬(CDR (QUOTE (A B C D)))␈↓␈↓ 	x␈↓¬(B C D)␈↓
␈↓ ↓H␈↓␈↓ ↓H(ivc)␈↓ α8␈↓↓cons[␈↓¬A,(B C D)␈↓↓]␈↓␈↓ ∧x␈↓¬(CONS (QUOTE A) (QUOTE (B C D)))␈↓␈↓ 	x␈↓¬(A B C D)␈↓

␈↓ ↓H␈↓␈↓ ↓H(v)␈↓ α8␈↓αat|␈↓␈↓¬A␈↓␈↓ ∧x␈↓¬(ATOM (QUOTE A))␈↓␈↓ 	x␈↓¬T␈↓
␈↓ ↓H␈↓␈↓ α8␈↓αat|␈↓␈↓¬(A . B)␈↓␈↓ ∧x␈↓¬(ATOM (QUOTE (A . B))␈↓␈↓ 	x␈↓¬NIL␈↓

␈↓ ↓H␈↓␈↓ ↓H(vi)␈↓ α8␈↓¬A␈↓ ␈↓αeq␈↓ ␈↓¬A␈↓␈↓ ∧x␈↓¬(EQ (QUOTE A) (QUOTE A))␈↓␈↓ 	x␈↓¬T␈↓
␈↓ ↓H␈↓␈↓ α8␈↓¬A␈↓ ␈↓αeq␈↓ ␈↓¬B␈↓␈↓ ∧x␈↓¬(EQ (QUOTE A) (QUOTE B))␈↓␈↓ 	x␈↓¬NIL␈↓
␈↓ ↓H␈↓␈↓ α8␈↓¬(A . B)␈↓ ␈↓αeq␈↓ ␈↓¬(A . B)␈↓␈↓ ∧x␈↓¬(EQ (QUOTE (A . B)) (QUOTE (A . B)))␈↓␈↓ 	x␈↓¬?␈↓



␈↓ ↓H␈↓␈↓ α∨␈↓αTable 1.␈↓  Examples of LISP terms in external and internal notation and their values.


␈↓ ↓H␈↓        We␈α⊂now␈α∂describe␈α⊂the␈α⊂basic␈α∂LISP␈α⊂programs␈α∂and␈α⊂give␈α⊂their␈α∂external␈α⊂and␈α⊂internal␈α∂names.
␈↓ ↓H␈↓The␈α
external␈α
names␈α∞are␈α
generally␈α
abbreviations␈α∞for␈α
the␈α
internal␈α
names␈α∞and␈α
will␈α
appear␈α∞in␈α
bold
␈↓ ↓H␈↓face type.

␈↓ ↓H␈↓        We␈α
begin␈αwith␈α
the␈α
programs␈αfor␈α
the␈α
selector␈αfunctions.␈α
 ␈↓↓car␈↓␈α
is␈αrepresented␈α
externally␈αby␈α
  ␈↓αa␈↓  
␈↓ ↓H␈↓and␈αinternally␈αby␈α␈↓¬CAR.␈α␈↓␈αWhen␈αapplied␈αto␈αa␈αnon-atomic␈αlist␈αstructure␈αthe␈αresult␈αis␈αthe␈αa-part.␈α See
␈↓ ↓H␈↓Table␈α1␈α(iia)␈αand␈α
(iiia)␈αfor␈αexamples.␈α ␈↓↓cdr␈↓␈α
is␈αrepresented␈αexternally␈αby␈α
  ␈↓αd␈↓  ␈αand␈αinternally␈αby␈α
␈↓¬CDR.
␈↓ ↓H␈↓¬␈↓ When␈αapplied␈αto␈αa␈αnon-atomic␈αlist␈αstructure␈αthe␈αresult␈αis␈αthe␈αd-part.␈α See␈αTable␈α1␈α(iid)␈αand␈α
(iiid).
␈↓ ↓H␈↓The␈α⊂action␈α⊂of␈α⊂the␈α⊂selector␈α⊂operations␈α⊂on␈α∂atoms␈α⊂is␈α⊂not␈α⊂defined␈α⊂in␈α⊂basic␈α⊂LISP.␈α⊂ However,␈α∂most
␈↓ ↓H␈↓implementations assign a some "meaning" to such terms, possibly an error message.
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ 99


␈↓ ↓H␈↓        Since␈α∂lists␈α∞are␈α∂a␈α∂particular␈α∞kind␈α∂of␈α∂S-expression,␈α∞the␈α∂action␈α∂of␈α∞the␈α∂selector␈α∂operations␈α∞on
␈↓ ↓H␈↓non-empty␈α⊂lists␈α⊂is␈α∂also␈α⊂determined␈α⊂by␈α⊂the␈α∂above␈α⊂definitions.␈α⊂ In␈α∂particular␈α⊂ ␈↓αa␈↓ ␈α⊂selects␈α⊂the␈α∂first
␈↓ ↓H␈↓element␈α
of␈α∞the␈α
list␈α∞and␈α
 ␈↓αd␈↓ ␈α∞selects␈α
the␈α∞rest␈α
of␈α∞the␈α
list␈α∞(e.g.␈α
the␈α∞list␈α
formed␈α∞by␈α
removing␈α∞that␈α
first
␈↓ ↓H␈↓element).␈αSee␈αTable␈α1␈α(iva)␈α
and␈α(ivd).␈α Recall␈α that␈αthe␈αa-part␈α
of␈αa␈αnon-empty␈αlist␈αcan␈αbe␈α
any␈αS-
␈↓ ↓H␈↓expression,␈α⊃while␈α⊂the␈α⊃d-part␈α⊂is␈α⊃always␈α⊃a␈α⊂list.␈α⊃ Thus␈α⊂the␈α⊃selectors␈α⊂behave␈α⊃symmetrically␈α⊃on␈α⊂S-
␈↓ ↓H␈↓expressions␈α∂but␈α∂unsymmetrically␈α∂on␈α∂lists␈α∂reflecting␈α∂the␈α∂symmetry␈α∂properties␈α∂of␈α∂the␈α⊂list␈α∂structure
␈↓ ↓H␈↓representation of S-expressions and lists.

␈↓ ↓H␈↓[Historical␈αnote:␈αthe␈αnames␈α"CAR"␈αand␈α"CDR"␈αstood␈αfor␈α"contents␈αof␈αthe␈αaddress␈αpart␈αof␈αregister"
␈↓ ↓H␈↓and␈α∞"contents␈α∞of␈α∞the␈α∞decrement␈α∞part␈α∞of␈α∞register"␈α∞in␈α∞a␈α∞1957␈α∞precursor␈α∞of␈α∞LISP␈α∞projected␈α∞for␈α∞the
␈↓ ↓H␈↓IBM 704 computer.  The names have persisted for lack of a clearly preferable alternative.]

␈↓ ↓H␈↓        ␈↓↓cons␈↓␈αis␈α
represented␈αexternally␈αby␈α
␈↓αcons␈↓␈αor␈α(more␈α
often)␈αby␈αan␈α
infixed␈α`` . ''␈αand␈α
internally␈αby
␈↓ ↓H␈↓␈↓¬CONS.␈α
␈↓␈α
 The␈αprogram␈α
for␈α
␈↓↓cons␈↓␈αmust␈α
have␈α
access␈α
to␈αa␈α
supply␈α
of␈αcons-cells␈α
called␈α
the␈α
free␈αstorage
␈↓ ↓H␈↓list.␈α Given␈αpointers␈αto␈αtwo␈αexisting␈αlist␈αstructures,␈αthe␈αprogram␈αremoves␈αa␈αcons-cell␈αfrom␈αthe␈αfree
␈↓ ↓H␈↓storage␈αlist␈αand␈αputs␈αthe␈αfirst␈αpointer␈α(address)␈α
into␈αthe␈αa-part␈αand␈αthe␈αsecond␈αinto␈αthe␈α
d-part␈αof
␈↓ ↓H␈↓this␈α∂cell.␈α⊂ The␈α∂result␈α⊂is␈α∂the␈α⊂S-expression␈α∂represented␈α⊂by␈α∂the␈α⊂pointer␈α∂to␈α⊂the␈α∂new␈α⊂cons-cell.␈α∂ See
␈↓ ↓H␈↓Table 1 (iic) and (iiic).

␈↓ ↓H␈↓        We␈α
see␈α
that␈α
for␈α
lists,␈α ␈↓↓cons␈↓ ␈α
is␈α
a␈α
prefixing␈α
operation.␈α Namely,␈α
if␈α
the␈α
second␈α
argument␈α
is␈αa
␈↓ ↓H␈↓list␈α∞then␈α∞the␈α∂result␈α∞of␈α∞applying␈α∂␈↓↓cons␈↓␈α∞is␈α∞the␈α∞list␈α∂obtained␈α∞by␈α∞putting␈α∂the␈α∞first␈α∞argument␈α∂onto␈α∞the
␈↓ ↓H␈↓front of that list.  See Table 1 (ivc).

␈↓ ↓H␈↓[Note␈α
that␈α
giving␈αthe␈α
same␈α
pair␈αof␈α
pointers␈α
to␈αthe␈α
␈↓↓cons␈↓␈α
program␈αa␈α
second␈α
time␈αwill␈α
result␈α
in␈αthe
␈↓ ↓H␈↓construction␈α∩of␈α∪a␈α∩list␈α∩structure␈α∪distinct␈α∩from␈α∩the␈α∪first,␈α∩although␈α∩both␈α∪represent␈α∩the␈α∪same␈α∩S-
␈↓ ↓H␈↓expression.]

␈↓ ↓H␈↓        The␈αpredicate␈α
␈↓↓atom␈↓␈αis␈α
represented␈αexternally␈α
by␈α␈↓αat␈↓␈αand␈α
internally␈αby␈α
␈↓¬ATOM.␈α␈↓␈α
The␈αprogram
␈↓ ↓H␈↓for␈α␈↓↓atom␈↓␈αwhen␈αgiven␈αa␈αpointer␈αto␈αa␈αlist␈αstructure␈αdetermines␈αwhether␈αthat␈αlist␈αstructure␈αrepresents
␈↓ ↓H␈↓and␈αatom␈α
or␈αnot.␈αThe␈α
result␈αis␈α
␈↓¬T␈↓␈αif␈αthe␈α
list␈αstructure␈α
is␈αatomic␈αand␈α
␈↓¬NIL␈↓␈αotherwise.␈α
 See␈αTable␈α1␈α
(v).
␈↓ ↓H␈↓We␈α∂see␈α∂that␈α∂the␈α∂truthvalues␈α⊂␈↓αtrue␈↓␈α∂and␈α∂␈↓αfalse␈↓␈α∂are␈α∂represented␈α∂in␈α⊂LISP␈α∂by␈α∂the␈α∂atoms␈α∂␈↓¬T␈↓␈α⊂and␈α∂␈↓¬NIL␈↓
␈↓ ↓H␈↓respectively.

␈↓ ↓H␈↓        The␈α⊃predicate␈α∩␈↓↓eq␈↓␈α⊃is␈α∩represented␈α⊃externally␈α∩by␈α⊃the␈α∩infix␈α⊃␈↓αeq␈↓␈α∩and␈α⊃internally␈α∩by␈α⊃␈↓¬EQ.␈α∩␈↓␈α⊃As
␈↓ ↓H␈↓mentioned␈α
earlier,␈α
it␈α
is␈α
only␈α
required␈α
to␈α
test␈α
for␈α
equality␈α
of␈α
atoms.␈α
 The␈α
program␈α
for␈α∞␈↓↓eq␈↓␈α
actually
␈↓ ↓H␈↓does␈αa␈αbit␈αmore.␈α Namely,␈αgiven␈αpointers␈αto␈α
two␈αlist␈αstructures␈αit␈αcompares␈αthese␈αaddresses.␈α If␈α
they
␈↓ ↓H␈↓are␈α∂equal␈α∂the␈α∂result␈α∞is␈α∂␈↓¬T␈↓,␈α∂otherwise␈α∂it␈α∂is␈α∞␈↓¬NIL␈↓.␈α∂ Since␈α∂each␈α∂atom␈α∞is␈α∂represented␈α∂by␈α∂a␈α∂unique␈α∞list
␈↓ ↓H␈↓structure,␈α∞the␈α
comparison␈α∞is␈α
indeed␈α∞a␈α
test␈α∞of␈α
equality␈α∞if␈α
at␈α∞least␈α
one␈α∞of␈α
the␈α∞arguments␈α∞is␈α
atomic.
␈↓ ↓H␈↓More␈αgenerally␈αif␈αtwo␈αlist␈αstructures␈αhave␈αthe␈αsame␈αaddress,␈αthey␈αrepresent␈αthe␈αsame␈αS-expression.
␈↓ ↓H␈↓The␈αconverse␈αis␈αfalse␈αbecause␈αthere␈αis␈αno␈αguarantee␈αthat␈αthe␈αsame␈αS-expression␈αis␈αnot␈αrepresented
␈↓ ↓H␈↓by␈α
two␈α
different␈α
list␈αstructures.␈α
 (Two␈α
applications␈α
of␈α␈↓↓cons␈↓␈α
to␈α
a␈α
pair␈αof␈α
list␈α
structures␈α
in␈αmost␈α
LISPs
␈↓ ↓H␈↓will␈αresult␈αin␈αtwo␈αlist␈αstructures␈αrepresenting␈αthe␈αsame␈αS-expression,␈αbut␈αwith␈αdifferent␈αaddresses.)
␈↓ ↓H␈↓Equality␈α
of␈α
S-expressions␈α
is␈α
tested␈α
by␈α
a␈α
program␈α
based␈α
on␈α
 ␈↓↓eq␈↓␈α
which␈α
we␈α
will␈α
describe␈α
in␈α
a␈αlater
␈↓ ↓H␈↓section.␈α∂ If␈α∂this␈α∂program␈α∂were␈α∂used␈α∂instead␈α⊂of␈α∂␈↓↓eq␈↓␈α∂as␈α∂a␈α∂basic␈α∂function,␈α∂LISP␈α∂would␈α⊂be␈α∂logically
␈↓ ↓H␈↓simpler, but many programs would be slower.

␈↓ ↓H␈↓        This␈α∂concludes␈α∂the␈α∂description␈α∞of␈α∂the␈α∂basic␈α∂LISP␈α∞programs.␈α∂ To␈α∂conclude␈α∂the␈α∂section␈α∞we
␈↓ ↓H␈↓give some further notational extensions, conventions and abbreviations.

␈↓ ↓H␈↓        Besides␈α
the␈α
basic␈α
functions␈αof␈α
LISP,␈α
there␈α
will␈αbe␈α
user-defined␈α
functions.␈α
 We␈αhaven't␈α
given
␈↓ ↓H␈↓10␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓the␈α∞mechanism␈α
for␈α∞function␈α
definition␈α∞yet,␈α
but␈α∞suppose␈α
a␈α∞function␈α
␈↓↓subst␈↓␈α∞taking␈α∞three␈α
arguments
␈↓ ↓H␈↓has␈α→been␈α~defined.␈α→ It␈α→may␈α~be␈α→used␈α~in␈α→terms␈α→like␈α~ ␈↓↓subst[x,y,z]␈↓ ␈α→having␈α~internal␈α→form
␈↓ ↓H␈↓ ␈↓¬(SUBST X Y Z)␈↓.

␈↓ ↓H␈↓        As␈α∂in␈α∞other␈α∂programming␈α∞languages␈α∂and␈α∞in␈α∂mathematics␈α∞generally,␈α∂application␈α∂terms␈α∞can
␈↓ ↓H␈↓have␈α∞arbitrary␈α∞terms␈α∂as␈α∞arguments,␈α∞not␈α∂just␈α∞variables␈α∞and␈α∞constants.␈α∂ Thus␈α∞we␈α∞have␈α∂terms␈α∞like
␈↓ ↓H␈↓ ␈↓↓[subst[x,y,␈↓αa|␈↓↓z]␈α∂.␈α∂subst[x,y,␈↓αd|␈↓↓z]]␈↓ ␈α∂involving␈α∞a␈α∂user␈α∂defined␈α∂function␈α∞␈↓↓subst.␈↓␈α∂ Its␈α∂internal␈α∂form␈α∞is
␈↓ ↓H␈↓ ␈↓¬(CONS (SUBST X Y (CAR Z)) (SUBST X Y (CDR Z)))␈↓.

␈↓ ↓H␈↓        ␈↓αn|␈↓␈↓↓x␈↓␈α∞ is␈α∞an␈α∞abbreviation␈α∞for␈α∞ ␈↓↓x␈α∞␈↓αeq␈↓↓␈α∞␈↓¬NIL␈↓↓␈↓.␈α
 It␈α∞rates␈α∞a␈α∞special␈α∞notation␈α∞because␈α∞ ␈↓¬NIL␈↓ ␈α∞plays␈α
the
␈↓ ↓H␈↓same␈αrole␈αamong␈α
lists␈αthat␈αzero␈α
plays␈αamong␈αnumbers,␈αand␈α
list␈αvariables␈αoften␈α
have␈αto␈αbe␈αtested␈α
to
␈↓ ↓H␈↓see if their value is ␈↓¬NIL␈↓.  Its internal form is ␈↓¬(NULL X)␈↓.

␈↓ ↓H␈↓    The␈αnotation␈α ␈↓↓list[x1, x2, . . . ,xn]␈↓ ␈αis␈αused␈αto␈αdenote␈αthe␈αcomposition␈αof␈α␈↓↓cons␈↓'s␈αthat␈αforms␈αa
␈↓ ↓H␈↓list␈α∂from␈α∞its␈α∂elements.␈α∂ Thus␈α∞ ␈↓↓list[x, y, z]␈↓ ␈α∂denotes␈α∞ ␈↓↓cons[x, cons[y, cons[z, ␈↓¬NIL␈↓↓]]]␈↓ ␈α∂and␈α∂forms␈α∞a
␈↓ ↓H␈↓list␈α
of␈αthree␈α
elements␈αout␈α
of␈αthe␈α
quantities␈α
␈↓↓x,␈↓␈α␈↓↓y,␈↓␈α
and␈α␈↓↓z.␈↓␈α
 We␈αoften␈α
write␈α ␈↓↓<x, y, . . ., z>␈↓ ␈α
instead
␈↓ ↓H␈↓of␈α⊂ ␈↓↓list[x, y, . . . , z]␈↓ ␈α⊃when␈α⊂this␈α⊂will␈α⊃not␈α⊂cause␈α⊂confusion.␈α⊃ The␈α⊂experienced␈α⊃implementer␈α⊂of
␈↓ ↓H␈↓programming␈α∩languages␈α∪will␈α∩expect␈α∪that␈α∩since␈α∪␈↓↓list␈↓␈α∩has␈α∪a␈α∩variable␈α∪number␈α∩of␈α∪arguments,␈α∩its
␈↓ ↓H␈↓implementation␈αwill␈αpose␈αproblems.␈α That␈αis␈αindeed␈αtrue.␈α The␈αinternal␈αform␈αof␈α ␈↓↓<x, y, . . ., z>␈↓ 
␈↓ ↓H␈↓is  ␈↓¬(LIST X Y . . . Z)␈↓.

␈↓ ↓H␈↓        Compositions␈αof␈α␈↓αa␈↓␈αand␈α␈↓αd␈↓␈αare␈αwritten␈αby␈αconcatenating␈αthe␈αletters␈α␈↓αa␈↓␈αand␈α␈↓αd␈↓.␈α Thus,␈αit␈αis␈αeasily
␈↓ ↓H␈↓seen␈α
that␈α∞␈↓αad|␈↓␈↓↓u␈↓␈α
denotes␈α∞the␈α
second␈α∞element␈α
of␈α∞the␈α
list␈α∞␈↓↓u,␈↓␈α
and␈α∞␈↓αadd|␈↓␈↓↓u␈↓␈α
denotes␈α∞the␈α
third␈α∞element␈α
of
␈↓ ↓H␈↓that list.  The internal names of these functions are ␈↓¬CADR ␈↓and ␈↓¬CADDR ␈↓respectively.

␈↓ ↓H␈↓        In␈α
external␈α
language,␈α∞we␈α
often␈α
omit␈α∞brackets␈α
for␈α
functions␈α∞of␈α
one␈α
argument.␈α∞ Thus␈α
 ␈↓↓alt x␈↓ 
␈↓ ↓H␈↓stands␈α∞for␈α∞ ␈↓↓alt[x]␈↓, ␈α∞and␈α∞ ␈↓↓alt alt x␈↓ ␈α∞stands␈α∞for␈α∞ ␈↓↓alt[alt[x]]␈↓ .␈α∞ This␈α∞convention␈α∞was␈α∞also␈α∞used␈α∞in␈α∞the
␈↓ ↓H␈↓descriptions of ␈↓αa␈↓, ␈↓αd␈↓, ␈↓αat␈↓, and ␈↓αn␈↓.



␈↓ ↓H␈↓α␈↓ ε⊂Exercise

␈↓ ↓H␈↓1.  What is the value of ␈↓↓< ␈↓¬A ␈↓↓. ␈↓αa|␈↓↓␈↓¬(B . C)␈↓↓, ␈↓¬D ␈↓↓. ␈↓αd|␈↓↓␈↓¬(B . C)␈↓↓>␈↓?

␈↓ ↓H␈↓2.  What combinations of ␈↓αa␈↓ and ␈↓αd␈↓ select ␈↓¬(X Y)␈↓ and ␈↓¬(CONS X Y)␈↓ respectively from

␈↓ ↓H␈↓␈↓ βP␈↓↓␈↓¬((LAMBDA (X Y) (CONS X Y)) (QUOTE A) (QUOTE B))␈↓↓␈↓ 



␈↓ ↓H␈↓6.  ␈↓αConditional terms.␈↓


␈↓ ↓H␈↓        When␈α∩the␈α∩value␈α⊃of␈α∩a␈α∩function␈α∩depends␈α⊃on␈α∩whether␈α∩a␈α∩certain␈α⊃predicate␈α∩is␈α∩true␈α∩of␈α⊃the
␈↓ ↓H␈↓arguments,␈α∂conditional␈α∂terms␈α∂are␈α∂used␈α∂to␈α∂describe␈α∂the␈α∂function.␈α∂ In␈α∂external␈α∂language␈α⊂a␈α∂simple
␈↓ ↓H␈↓conditional term has the form

␈↓ ↓H␈↓6.1)␈↓ ¬/␈↓↓␈↓αif␈↓↓ p ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ b        ␈↓ 
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *11


␈↓ ↓H␈↓where␈α␈↓↓p␈↓␈αis␈αa␈αpropositional␈αterm␈αand␈α␈↓↓a␈↓␈αand␈α␈↓↓b␈↓␈αcan␈αbe␈αany␈αterms.␈α By␈αpropositional␈αterm␈αwe␈αmean␈αa
␈↓ ↓H␈↓term␈α∂whose␈α⊂value␈α∂represents␈α∂a␈α⊂truthvalue␈α∂(␈↓αtrue␈↓␈α⊂or␈α∂␈↓αfalse␈↓).␈α∂ A␈α⊂(program␈α∂computing␈α⊂a)␈α∂predicate
␈↓ ↓H␈↓applied␈αto␈αa␈α
list␈αof␈αarguments␈α
is␈αa␈αpropositional␈α
term.␈α We␈αwill␈α
explain␈αhow␈αto␈α
form␈αsuch␈αterms␈α
in
␈↓ ↓H␈↓general in the next section.

␈↓ ↓H␈↓The␈αconditional␈αterm␈α(6.1)␈αis␈αevaluated␈αas␈αfollows:␈α
first␈α ␈↓↓p␈↓␈α is␈αevaluated␈αand␈αdetermined␈αto␈αbe␈α
true
␈↓ ↓H␈↓or␈α
false.␈α
 If␈α␈↓↓p␈↓␈α
is␈α
true,␈αthen␈α
␈↓↓a␈↓␈α
is␈αevaluated␈α
and␈α
its␈αvalue␈α
is␈α
the␈αvalue␈α
of␈α
the␈αexpression.␈α
 If␈α
␈↓↓p␈↓␈αis␈α
false,
␈↓ ↓H␈↓then␈α␈↓↓b␈↓␈αis␈αevaluated␈αand␈αits␈αvalue␈αis␈αthe␈αvalue␈αof␈αthe␈αexpression.␈α Note␈αthat␈αif␈α␈↓↓p␈↓␈αis␈αtrue,␈α␈↓↓b␈↓␈αis␈αnever
␈↓ ↓H␈↓evaluated,␈αand␈αif␈α␈↓↓p␈↓␈αis␈αfalse,␈αthen␈α␈↓↓a␈↓␈αis␈αnever␈αevaluated.␈α The␈αimportance␈αof␈αthis␈αwill␈αbe␈αexplained
␈↓ ↓H␈↓later.

␈↓ ↓H␈↓        A␈α∞familiar␈α∂function␈α∞that␈α∞can␈α∂be␈α∞written␈α∞conveniently␈α∂using␈α∞conditional␈α∞expressions␈α∂is␈α∞the
␈↓ ↓H␈↓absolute value of a real number.  We have

␈↓ ↓H␈↓6.2)␈↓ ¬≤␈↓↓|x| = ␈↓αif␈↓↓ x<0 ␈↓αthen␈↓↓ -x ␈↓αelse␈↓↓ x␈↓. 

␈↓ ↓H␈↓[Remark:   The common mathematical notation for such definitions is

␈↓"␈↓ ↓H␈↓␈↓ ¬x␈↓π/␈↓  ␈↓↓x␈↓  if ␈↓↓x ≥ 0␈↓
␈↓"␈↓ ↓H␈↓␈↓ ¬_␈↓↓|x|␈↓ =␈↓ ¬x␈↓π[␈↓
␈↓"␈↓ ↓H␈↓␈↓ ¬x␈↓π\␈↓ ␈↓↓-x␈↓  otherwise,


␈↓ ↓H␈↓but the right hand side is not allowed to be part of another expression.]

␈↓ ↓H␈↓More generally a conditional term has the form

␈↓ ↓H␈↓6.3)␈↓ β↑␈↓↓␈↓αif␈↓↓ p ␈↓αthen␈↓↓ a ␈↓αelse␈↓↓ ␈↓αif␈↓↓ q ␈↓αthen␈↓↓ b . . .  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ s ␈↓αthen␈↓↓ d ␈↓αelse␈↓↓ e␈↓. 

␈↓ ↓H␈↓There␈α∞can␈α∞be␈α∞any␈α∂number␈α∞of␈α∞clauses.␈α∞The␈α∞value␈α∂is␈α∞determined␈α∞by␈α∞evaluating␈α∂the␈α∞propositional
␈↓ ↓H␈↓terms␈α␈↓↓p,␈↓␈α␈↓↓q,␈↓␈αetc.␈αuntil␈αa␈αtrue␈αterm␈αis␈α
found,␈αthe␈αvalue␈αis␈αthen␈αthat␈αof␈αthe␈αterm␈αfollowing␈α
the␈αnext
␈↓ ↓H␈↓␈↓αthen␈↓.␈α If␈αnone␈αof␈αthe␈α
propositional␈αterms␈αis␈αtrue,␈αthen␈αthe␈α
value␈αis␈αthat␈αof␈αthe␈αterm␈α
following␈αthe
␈↓ ↓H␈↓last ␈↓αelse␈↓.

␈↓ ↓H␈↓        Figure␈α_8␈α→shows␈α_an␈α_example␈α→of␈α_a␈α_(numeric)␈α→function␈α_defined␈α_using␈α→a␈α_conditional
␈↓ ↓H␈↓expression.
␈↓ ↓H␈↓12␈↓ εβChapter  I␈↓ H






␈↓"␈↓ ↓H␈↓                                           (0,1)
␈↓"␈↓ ↓H␈↓                                         ≤'`≥
␈↓"␈↓ ↓H␈↓                                       ≤'    `≥
␈↓"␈↓ ↓H␈↓            ↑                        ≤'        `≥
␈↓"␈↓ ↓H␈↓            ~                      ≤'            `≥
␈↓"␈↓ ↓H␈↓     tri[x] ~     αααααααααααααααα'                `αααααααααααααααα
␈↓"␈↓ ↓H␈↓            ~                  (-1,0)            (1,0)
␈↓"␈↓ ↓H␈↓             ααα→
␈↓"␈↓ ↓H␈↓              x


␈↓"␈↓ ↓H␈↓␈↓ αr␈↓↓tri[x] = ␈↓αif␈↓↓ x<-1 ␈↓αthen␈↓↓ 0 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ x<0 ␈↓αthen␈↓↓ 1+x ␈↓αelse␈↓↓ ␈↓αif␈↓↓ x<1 ␈↓αthen␈↓↓ 1-x ␈↓αelse␈↓↓ 0␈↓.

␈↓ ↓H␈↓␈↓ ∧t␈↓αFigure 8.␈↓  The triangle function.



␈↓ ↓H␈↓        The conditional term in internal language has the form

␈↓ ↓H␈↓6.4)␈↓ ∧g␈↓↓␈↓¬(COND ␈↓↓(p1 e1) (p2 e2) ... (pn en)␈↓¬)␈↓↓␈↓ 

␈↓ ↓H␈↓with␈α
any␈α
number␈αof␈α
␈↓↓p-e␈↓␈α
pairs.␈α
 Its␈αvalue␈α
is␈α
determined␈α
by␈αevaluating␈α
the␈α
␈↓↓p␈↓'s␈α
successively␈αuntil␈α
one
␈↓ ↓H␈↓is␈αfound␈αwith␈α
a␈αvalue␈αcorresponding␈α
to␈α␈↓αtrue␈↓.␈α The␈αvalue␈α
of␈αthe␈αcorresponding␈α
␈↓↓e␈↓␈αis␈αthen␈α
taken␈αas
␈↓ ↓H␈↓the␈α
value␈α
of␈α
the␈α
conditional␈α
term.␈α
 If␈α
none␈α
of␈α
the␈α
␈↓↓p␈↓'s␈α
is␈α
true,␈α
then␈α
the␈α
value␈α
of␈α
the␈αconditional␈α
term
␈↓ ↓H␈↓is␈αundefined.␈α (In␈αsome␈αimplementations␈αthe␈αconditional␈αterm␈αis␈αgiven␈αa␈αdefault␈αvalue␈αsuch␈αas␈α
␈↓¬NIL␈↓
␈↓ ↓H␈↓if none of the ␈↓↓p␈↓'s are true.)  

␈↓ ↓H␈↓[Remark:␈α∪In␈α∩the␈α∪internal␈α∪form,␈α∩all␈α∪the␈α∩␈↓↓e␈↓'s␈α∪are␈α∪treated␈α∩the␈α∪same␈α∩which␈α∪makes␈α∪programs␈α∩for
␈↓ ↓H␈↓interpreting␈α
or␈α∞compiling␈α
conditional␈α∞expressions␈α
easier␈α∞to␈α
write.␈α∞ This␈α
is␈α∞another␈α
way␈α∞in␈α
which
␈↓ ↓H␈↓expressions in internal language are made in a more regular way than in external language.  ]

␈↓ ↓H␈↓        Conditional expressions may be compounded with functions to get terms like

␈↓ ↓H␈↓6.5)␈↓ ∧2␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . append[␈↓αd|␈↓↓u, v]      ␈↓ 

␈↓ ↓H␈↓involving a yet to be defined function ␈↓↓append.␈↓  The internal form of this is

␈↓ ↓H␈↓6.6)␈↓ αy␈↓↓␈↓¬(COND ((NULL U) V) (T (CONS (CAR U) (APPEND (CDR U) V))))␈↓↓.␈↓ 

␈↓ ↓H␈↓One␈α
would␈αnormally␈α
have␈αexpected␈α
to␈αwrite␈α
␈↓¬(QUOTE␈α
T)␈↓,␈αbut␈α
there␈αis␈α
a␈αspecial␈α
convention␈α
that␈α␈↓¬T␈↓
␈↓ ↓H␈↓may␈α∂be␈α∂written␈α∂without␈α⊂␈↓¬QUOTE.␈α∂␈↓␈α∂This␈α∂convention␈α∂applies␈α⊂to␈α∂␈↓¬NIL␈↓␈α∂also.␈α∂ This␈α∂means␈α⊂that␈α∂these
␈↓ ↓H␈↓symbols␈αcannot␈αbe␈αused␈αas␈αvariables.␈α As␈α
mentioned␈αin␈αsection␈α␈↓π∞␈↓␈α5,␈α␈↓¬T␈↓␈αrepresents␈α
the␈αpropositional
␈↓ ↓H␈↓constant␈α∞␈↓αtrue␈↓,␈α∞i.e.␈α∞it␈α∞is␈α∞always␈α∞true␈α∞so␈α∞that␈α
it␈α∞is␈α∞impossible␈α∞to␈α∞"fall␈α∞off␈α∞the␈α∞end"␈α∞of␈α∞a␈α
conditional
␈↓ ↓H␈↓expression␈α∂with␈α∂␈↓¬T␈↓␈α⊂as␈α∂its␈α∂last␈α∂␈↓↓p.␈↓␈α⊂ It␈α∂is␈α∂the␈α∂translation␈α⊂into␈α∂internal␈α∂form␈α∂of␈α⊂the␈α∂final␈α∂␈↓αelse␈↓␈α⊂of␈α∂a
␈↓ ↓H␈↓conditional expression in external form.
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *13


␈↓ ↓H␈↓7.  ␈↓αPropositional terms.␈↓


␈↓ ↓H␈↓        As␈α∃mentioned␈α∃in␈α∃the␈α∃previous␈α∃section,␈α∃propositional␈α∃terms␈α∃are␈α∃a␈α∃terms␈α∃whose␈α∃value
␈↓ ↓H␈↓represents␈αa␈αtruth␈αvalue.␈α The␈αsimplest␈αpropositional␈αterms␈αare␈αthe␈αconstants␈α␈↓¬T␈↓␈αand␈α␈↓¬NIL␈↓.␈α If␈α␈↓πf␈↓␈αis␈αa
␈↓ ↓H␈↓predicate␈αof␈αn-arguments␈αthen␈α␈↓↓␈↓πf␈↓↓[x1, . . . ,xn]␈↓␈αis␈αa␈αpropositional␈αterm.␈α Additional␈αpropositional
␈↓ ↓H␈↓terms␈α∃can␈α∃be␈α∃formed␈α∃by␈α∃combining␈α∃terms␈α∃already␈α∃formed␈α∃using␈α∃the␈α∃logical␈α⊗operations␈α∃of
␈↓ ↓H␈↓conjunction(␈↓αand␈↓),␈α
disjunction(␈↓αor␈↓)␈α
and␈α
negation(␈↓αnot␈↓).␈α
 Both␈α
␈↓αand␈↓␈α
and␈α
␈↓αor␈↓␈α
are␈α
associative,␈α
so␈α
we␈αcan
␈↓ ↓H␈↓write expressions like  ␈↓↓p1 ␈↓αand␈↓↓ p2 ␈↓αand␈↓↓ p3␈↓  without worrying about the grouping.

␈↓ ↓H␈↓        The␈α∩value␈α∪of␈α∩a␈α∩propositional␈α∪term␈α∩can␈α∩be␈α∪determined␈α∩using␈α∩the␈α∪truth␈α∩table␈α∪given␈α∩in
␈↓ ↓H␈↓Table 2.


␈↓ ↓H␈↓␈↓ αx␈↓¬T␈↓ ␈↓αand␈↓ ␈↓¬T␈↓  = ␈↓¬T␈↓␈↓ ¬x␈↓¬T␈↓ ␈↓αor␈↓ ␈↓¬T␈↓  = ␈↓¬T␈↓
␈↓ ↓H␈↓␈↓ αx␈↓¬T␈↓ ␈↓αand␈↓ ␈↓¬NIL␈↓ = ␈↓¬NIL␈↓␈↓ ¬x␈↓¬T␈↓ ␈↓αor␈↓ ␈↓¬NIL␈↓ = ␈↓¬T␈↓␈↓ λ8␈↓αnot␈↓ ␈↓¬T␈↓  = ␈↓¬NIL␈↓
␈↓ ↓H␈↓␈↓ αx␈↓¬NIL␈↓ ␈↓αand␈↓ ␈↓¬T␈↓  = ␈↓¬NIL␈↓␈↓ ¬x␈↓¬NIL␈↓ ␈↓αor␈↓ ␈↓¬T␈↓  = ␈↓¬T␈↓␈↓ λ8␈↓αnot␈↓ ␈↓¬NIL␈↓ = ␈↓¬T␈↓
␈↓ ↓H␈↓␈↓ αx␈↓¬NIL␈↓ ␈↓αand␈↓ ␈↓¬NIL␈↓ = ␈↓¬NIL␈↓␈↓ ¬x␈↓¬NIL␈↓ ␈↓αor␈↓ ␈↓¬NIL␈↓ = ␈↓¬NIL␈↓


␈↓ ↓H␈↓␈↓ ∧!␈↓αTable 2.␈↓  Truth table for propositional terms.


␈↓ ↓H␈↓        The evaluation of propositional terms is defined using conditional terms as follows:

␈↓ ↓H␈↓7.1)␈↓ ¬α␈↓↓p ␈↓αand␈↓↓ q = ␈↓αif␈↓↓ p ␈↓αthen␈↓↓ q ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓␈↓

␈↓ ↓H␈↓7.2)␈↓ ¬↔␈↓↓ p ␈↓αor␈↓↓ q = ␈↓αif␈↓↓ p ␈↓αthen␈↓↓ p ␈↓αelse␈↓↓ q␈↓

␈↓ ↓H␈↓7.3)␈↓ ¬¬␈↓↓  ␈↓αnot␈↓↓ p = ␈↓αif␈↓↓ p ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓¬T␈↓↓␈↓


␈↓ ↓H␈↓The internal forms of these definitions are

␈↓ ↓H␈↓␈↓ ∧H␈↓↓␈↓¬(AND P Q) = (COND (P Q) (T NIL))␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓↓␈↓¬ (OR P Q) = (COND (P P) (T Q))  ␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓↓␈↓¬  (NOT P) = (COND (P NIL) (T T))␈↓↓␈↓


␈↓ ↓H␈↓Note␈α
that␈α
if␈α
␈↓↓p␈↓␈α
is␈α
false␈α
then␈α
 ␈↓↓p ␈↓αand␈↓↓ q = ␈↓¬NIL␈↓↓␈↓ ␈α
independent␈α
of␈α
the␈α
value␈α
of␈α
␈↓↓q,␈↓␈α
and␈α
likewise␈α
if␈α
␈↓↓p␈↓␈αis
␈↓ ↓H␈↓true,␈α
then␈α ␈↓↓p ␈↓αor␈↓↓ q = ␈↓¬T␈↓↓␈↓ ␈α
independent␈αof␈α
␈↓↓q.␈↓␈α If␈α
␈↓↓p␈↓␈αhas␈α
been␈αevaluated␈α
and␈αfound␈α
to␈αbe␈α
false,␈α
then␈α␈↓↓q␈↓
␈↓ ↓H␈↓does␈α∞not␈α∞have␈α∞to␈α∞be␈α∞evaluated␈α∞at␈α∞all␈α∞to␈α∂find␈α∞the␈α∞value␈α∞of␈α∞␈↓↓p ␈↓αand␈↓↓ q␈↓,␈α∞and,␈α∞in␈α∞fact,␈α∞LISP␈α∂does␈α∞not
␈↓ ↓H␈↓evaluate␈α⊃␈↓↓q␈↓␈α⊃in␈α⊃this␈α⊂case.␈α⊃ Similarly,␈α⊃␈↓↓q␈↓␈α⊃is␈α⊂not␈α⊃evaluated␈α⊃in␈α⊃evaluating␈α⊂␈↓↓p ␈↓αor␈↓↓ q␈↓␈α⊃if␈α⊃␈↓↓p␈↓␈α⊃is␈α⊃true.␈α⊂This
␈↓ ↓H␈↓procedure␈αis␈αin␈α
accordance␈αwith␈αthe␈αabove␈α
conditional␈αexpression␈αdescriptions␈αof␈α
these␈αoperators.
␈↓ ↓H␈↓The␈α∞importance␈α∞of␈α∞this␈α∂convention,␈α∞which␈α∞contrasts␈α∞with␈α∞that␈α∂of␈α∞ALGOL␈α∞60,␈α∞will␈α∂be␈α∞apparent
␈↓ ↓H␈↓later when we discuss recursive definitions of functions and predicates.

␈↓ ↓H␈↓        Propositional␈α
expressions␈α
can␈α
be␈α
combined␈α
with␈α
functions␈α
and␈α
conditional␈α
expressions␈α
to␈α
get
␈↓ ↓H␈↓terms like

␈↓ ↓H␈↓7.4)␈↓ ∧@␈↓↓␈↓αif␈↓↓ [␈↓αn|␈↓↓u ␈↓αor␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓u] ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . alt ␈↓αdd|␈↓↓u␈↓ 
␈↓ ↓H␈↓14␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓whose internal form is

␈↓ ↓H␈↓7.5)␈↓ ↓m␈↓↓␈↓¬  (COND ((OR (NULL U) (NULL (CDR U))) U) (T (CONS (CAR U) (ALT (CDDR U)))))␈↓↓␈↓. 

␈↓ ↓H␈↓        We␈αconclude␈αthis␈αsection␈αwith␈αa␈αcouple␈αremarks.␈α First,␈αthe␈αonly␈αS-expressions␈αthat␈αwe␈αhave
␈↓ ↓H␈↓said␈αrepresent␈αtruthvalues␈αare␈α␈↓¬T␈↓␈αand␈α␈↓¬NIL␈↓.␈α However,␈αmost␈αLISP␈αimplementations␈αregard␈αany␈αnon-
␈↓ ↓H␈↓␈↓¬NIL␈↓␈α∃S-expression␈α⊗as␈α∃representing␈α∃␈↓αtrue␈↓.␈α⊗ These␈α∃LISPs␈α∃do␈α⊗not␈α∃distinguish␈α⊗between␈α∃between
␈↓ ↓H␈↓propositional␈α⊃terms␈α⊃and␈α⊂more␈α⊃general␈α⊃terms.␈α⊂ Thus␈α⊃any␈α⊃term␈α⊂can␈α⊃appear␈α⊃in␈α⊂the␈α⊃␈↓↓p␈↓␈α⊃part␈α⊃of␈α⊂a
␈↓ ↓H␈↓conditional␈α∀term.␈α∪ Second,␈α∀most␈α∪LISP␈α∀implementations␈α∀allow␈α∪␈↓αand␈↓␈α∀and␈α∪␈↓αor␈↓␈α∀to␈α∀have␈α∪arbitrary
␈↓ ↓H␈↓numbers␈α⊃of␈α∩arguments.␈α⊃ Since␈α∩these␈α⊃operations␈α∩are␈α⊃associative,␈α∩this␈α⊃can␈α∩be␈α⊃viewed␈α∩as␈α⊃simply
␈↓ ↓H␈↓omitting parentheses as it won't matter how the arguments are grouped.



␈↓ ↓H␈↓8.  ␈↓αRecursive function definitions.␈↓


␈↓ ↓H␈↓        In␈α∞such␈α∞languages␈α
as␈α∞FORTRAN␈α∞and␈α
ALGOL,␈α∞computer␈α∞programs␈α
are␈α∞expressed␈α∞in␈α
the
␈↓ ↓H␈↓main␈α⊃as␈α⊃sequences␈α⊃of␈α⊃assignment␈α⊃statements␈α⊃and␈α⊃conditional␈α⊃␈↓αgo␈α⊃to␈↓'s.␈α⊃ In␈α⊃LISP,␈α⊃programs␈α⊃are
␈↓ ↓H␈↓mainly expressed in the form of recursively defined functions.  We begin with an example.

␈↓ ↓H␈↓        We␈αwant␈α
a␈αfunction␈α␈↓↓alt␈↓␈α
such␈αthat␈α␈↓↓alt u␈↓␈α
 gives␈αa␈α
list␈αwhose␈αelements␈α
are␈αalternate␈αelements␈α
of
␈↓ ↓H␈↓the list ␈↓↓u␈↓ beginning with the first.  For example we want

␈↓ ↓H␈↓␈↓ ∧}␈↓↓   alt ␈↓¬(A B C D E)␈↓↓ = ␈↓¬(A C E)␈↓↓␈↓,
␈↓ ↓H␈↓␈↓ ∧x␈↓↓ alt ␈↓¬((A B) (C D))␈↓↓ = ␈↓¬((A B))␈↓↓␈↓,
␈↓ ↓H␈↓␈↓ ¬!␈↓↓           alt ␈↓¬(A)␈↓↓ = ␈↓¬(A)␈↓↓,   ␈↓
␈↓ ↓H␈↓␈↓ ¬∂␈↓↓             alt ␈↓¬NIL␈↓↓ = ␈↓¬NIL␈↓↓.     ␈↓


␈↓ ↓H␈↓and in LISP we can define ␈↓↓alt␈↓ by the recursion equation

␈↓ ↓H␈↓8.1) ␈↓ ∧∃␈↓↓alt u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αor␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . alt ␈↓αdd|␈↓↓u␈↓. 

␈↓ ↓H␈↓In case you need a review of our precedence conventions, fully bracketed it looks like

␈↓ ↓H␈↓␈↓ β;␈↓↓alt[u] ← [␈↓αif␈↓↓ [␈↓αn|␈↓↓[u] ␈↓αor␈↓↓ ␈↓αn|␈↓↓[␈↓αd|␈↓↓[u]]] ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ [␈↓αa|␈↓↓[u] . alt[␈↓αdd|␈↓↓[u]]]]␈↓. 

␈↓ ↓H␈↓        The␈α⊃terms␈α∩appearing␈α⊃in␈α⊃the␈α∩recursion␈α⊃equation␈α⊃are␈α∩formed␈α⊃by␈α⊃the␈α∩methods␈α⊃previously
␈↓ ↓H␈↓discussed.␈α Notice␈αthat␈αthe␈αfunction␈α␈↓↓alt␈↓␈αoccurs␈αin␈αthe␈αright␈αhand␈αside␈αof␈αits␈αown␈αdefining␈αequation
␈↓ ↓H␈↓and␈αthat␈αwe␈αuse␈α"←"␈αinstead␈αof␈α"="␈α.␈α The␈αrecursion␈αequation␈αtells␈αus␈αthat␈α␈↓↓alt␈↓␈αis␈αa␈αfunction␈αof␈αone
␈↓ ↓H␈↓variable␈α∞␈↓↓u,␈↓␈α∞and␈α∞that␈α∞the␈α∞value␈α∞of␈α∞␈↓↓alt␈α∞u␈↓␈α∞for␈α∞some␈α∞particular␈α∞list␈α∞is␈α∞computed␈α∞by␈α∂evaluating␈α∞the
␈↓ ↓H␈↓right␈α
hand␈α
side␈α
of␈α
the␈αdefinition,␈α
substituting␈α
that␈α
list␈α
for␈α␈↓↓u␈↓␈α
whenever␈α
␈↓↓u␈↓␈α
is␈α
encountered␈α
and␈αre-
␈↓ ↓H␈↓evaluating␈αthe␈αright␈αhand␈αside␈αwith␈αa␈αnew␈α␈↓↓u␈↓␈αwhenever␈αa␈αterm␈α␈↓↓alt␈αe␈↓␈αis␈αencountered.␈α This␈αprocess
␈↓ ↓H␈↓is best illustrated by evaluating some expressions.

␈↓ ↓H␈↓        Consider␈α
evaluating␈α
␈↓↓alt ␈↓¬NIL␈↓↓␈↓.␈α
 We␈αdo␈α
it␈α
by␈α
evaluating␈α
the␈αexpression␈α
to␈α
the␈α
right␈α
of␈αthe␈α
 "←" 
␈↓ ↓H␈↓in␈α
(8.1)␈α
remembering␈α
that␈αthe␈α
variable␈α
␈↓↓u␈↓␈α
has␈α
the␈αvalue␈α
␈↓¬NIL␈↓.␈α
 A␈α
conditional␈α
expression␈αis␈α
evaluated
␈↓ ↓H␈↓by␈α∞first␈α∞evaluating␈α∂the␈α∞first␈α∞propositional␈α∂term␈α∞-␈α∞in␈α∞this␈α∂case␈α∞␈↓↓␈↓αn|␈↓↓u␈α∞␈↓αor␈↓↓␈α∂␈↓αn|␈↓↓␈↓αd|␈↓↓u␈↓.␈α∞This␈α∞expression␈α∂is␈α∞a
␈↓ ↓H␈↓disjunction␈α∂so␈α∂we␈α∂first␈α∂evaluate␈α∂the␈α∂first␈α∞disjunct,␈α∂namely␈α∂␈↓αn|␈↓␈↓↓u.␈↓␈α∂(Recall␈α∂the␈α∂convention␈α∂given␈α∞in
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *15


␈↓ ↓H␈↓section␈α␈↓π∞␈↓␈α7).␈α Since␈α ␈↓↓u␈α=␈α␈↓¬NIL␈↓↓␈↓,␈α ␈↓↓␈↓αn|␈↓↓u␈↓ ␈αis␈αtrue,␈αthe␈αdisjunction␈αis␈αtrue,␈αand␈αthe␈αvalue␈αof␈αthe␈αconditional
␈↓ ↓H␈↓expression␈αis␈αthe␈αvalue␈αof␈αthe␈αterm␈αafter␈αthe␈αfirst␈αtrue␈αpropositional␈αterm.␈αThis␈αterm␈αis␈α␈↓↓u,␈↓␈αand␈αits
␈↓ ↓H␈↓value␈α
is␈α␈↓¬NIL␈↓,␈α
so␈α
the␈αvalue␈α
of␈α ␈↓↓alt[␈↓¬NIL␈↓↓]␈↓ ␈α
is␈α
␈↓¬NIL␈↓.␈α Obeying␈α
the␈αrules␈α
about␈α
the␈αorder␈α
of␈αevaluation␈α
of
␈↓ ↓H␈↓terms␈α
in␈αconditional␈α
and␈αpropositional␈α
expressions␈αis␈α
important␈αin␈α
this␈αcase␈α
since␈αif␈α
we␈αdidn't␈α
obey
␈↓ ↓H␈↓these␈α∞rules,␈α∂we␈α∞might␈α∂find␈α∞ourselves␈α∂trying␈α∞to␈α∞evaluate␈α∂ ␈↓↓␈↓αn|␈↓↓␈↓αd|␈↓↓u␈↓ ␈α∞or␈α∂ ␈↓↓alt[␈↓αdd|␈↓↓u]␈↓,␈α∞and␈α∂since␈α∞␈↓↓u␈↓␈α∂is␈α∞␈↓¬NIL␈↓,
␈↓ ↓H␈↓neither␈α∞of␈α∞these␈α∞has␈α∞a␈α∞value.␈α∞ (An␈α∞attempt␈α∞to␈α∞evaluate␈α∞them␈α∞would␈α∞result␈α∞in␈α∞an␈α∞error␈α∞return␈α
in
␈↓ ↓H␈↓most LISPs.)

␈↓ ↓H␈↓        As␈α
a␈α
second␈α
example,␈α
consider␈α
 ␈↓↓alt ␈↓¬(A B)␈↓↓␈↓.␈α
 Since␈α
neither␈α
␈↓αn|␈↓␈↓↓u␈↓␈α
nor␈α
␈↓αn|␈↓␈↓αd|␈↓␈↓↓u␈↓␈α
is␈α
true␈α
in␈α
this␈αcase,
␈↓ ↓H␈↓the␈α
disjunction␈α
 ␈↓↓␈↓αn|␈↓↓u␈α␈↓αor␈↓↓␈α
␈↓αn|␈↓↓␈↓αd|␈↓↓u␈↓ ␈α
is␈αfalse␈α
and␈α
the␈α
value␈αof␈α
the␈α
expression␈αis␈α
the␈α
value␈αof␈α
 ␈↓↓␈↓αa|␈↓↓u . alt ␈↓αdd|␈↓↓u␈↓.
␈↓ ↓H␈↓␈↓αa|␈↓␈↓↓u␈↓␈α⊂has␈α⊂the␈α⊂value␈α⊂␈↓¬A,␈α⊂␈↓and␈α⊂␈↓αdd|␈↓␈↓↓u␈↓␈α⊂has␈α⊂the␈α∂value␈α⊂␈↓¬NIL␈↓,␈α⊂so␈α⊂we␈α⊂must␈α⊂now␈α⊂evaluate␈α⊂␈↓↓alt ␈↓¬NIL␈↓↓␈↓,␈α⊂and␈α∂we
␈↓ ↓H␈↓already␈αknow␈αthat␈αthis␈αis␈α␈↓¬NIL␈↓.␈α Therefore,␈αthe␈αvalue␈αof␈α ␈↓↓alt ␈↓¬(A B)␈↓↓␈↓ ␈αis␈αthat␈αof␈α ␈↓¬A . NIL␈↓ ␈αwhich␈αis
␈↓ ↓H␈↓ ␈↓¬(A)␈↓.

␈↓ ↓H␈↓        This evaluation is described by the following  sequence of equations:

␈↓ ↓H␈↓␈↓ αX␈↓↓alt ␈↓¬(A B)␈↓↓␈↓␈↓ βX␈↓↓= ␈↓αif␈↓↓ [␈↓αn|␈↓↓␈↓¬(A B)␈↓↓ ␈↓αor␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬(A B)␈↓↓] ␈↓αthen␈↓↓ ␈↓¬(A B)␈↓↓  ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬(A B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓αif␈↓↓ [␈↓αfalse␈↓↓ ␈↓αor␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬(A B)␈↓↓] ␈↓αthen␈↓↓ ␈↓¬(A B)␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬(A B)␈↓↓]      ␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓αif␈↓↓ ␈↓αfalse␈↓↓ ␈↓αthen␈↓↓ ␈↓¬(A B)␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬(A B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬(A B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓¬A ␈↓↓. alt[␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓¬A ␈↓↓. ␈↓αif␈↓↓ [␈↓αn|␈↓↓␈↓¬NIL␈↓↓ ␈↓αor␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬NIL␈↓↓] ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓  ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬NIL␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓¬A ␈↓↓. ␈↓αif␈↓↓ [␈↓αtrue␈↓↓ ␈↓αor␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬NIL␈↓↓] ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓  ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬NIL␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓¬A ␈↓↓. ␈↓αif␈↓↓ ␈↓αtrue␈↓↓ ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓  ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬NIL␈↓↓ . alt[␈↓αdd|␈↓↓␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓¬A ␈↓↓. ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓= ␈↓¬(A)␈↓↓␈↓.


␈↓ ↓H␈↓        This␈αis␈α
still␈αvery␈α
long-winded,␈αand␈αnow␈α
that␈αthe␈α
reader␈αunderstands␈α
the␈αorder␈αof␈α
evaluation
␈↓ ↓H␈↓of conditional and propositional expressions, we can proceed more briefly to evaluate

␈↓ ↓H␈↓␈↓ β(␈↓↓alt[␈↓¬(A B C D E)␈↓↓]␈↓␈↓ ¬_␈↓↓= ␈↓¬A ␈↓↓. alt[␈↓¬(C D E)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬A ␈↓↓. [␈↓¬C ␈↓↓. alt[␈↓¬(E)␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬A ␈↓↓. [␈↓¬(C E)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬(A C E)␈↓↓␈↓.


␈↓ ↓H␈↓        Here␈αare␈αthree␈αmore␈αexamples␈αof␈αrecursive␈αfunctions␈αand␈αtheir␈αapplication.␈α We␈αdefine␈α␈↓↓last␈↓
␈↓ ↓H␈↓by

␈↓ ↓H␈↓8.2) ␈↓ ∧U␈↓↓last u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ ␈↓αa|␈↓↓u ␈↓αelse␈↓↓ last ␈↓αd|␈↓↓u␈↓, 

␈↓ ↓H␈↓and we compute

␈↓ ↓H␈↓␈↓ βh␈↓↓last ␈↓¬(A B C)␈↓↓␈↓␈↓ ¬_␈↓↓= ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓␈↓¬(A B C)␈↓↓ ␈↓αthen␈↓↓ ␈↓αa|␈↓↓␈↓¬(A B C)␈↓↓  ␈↓αelse␈↓↓ last ␈↓αd|␈↓↓␈↓¬(A B C)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= last ␈↓¬(B C)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= last ␈↓¬(C)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬C␈↓↓␈↓.
␈↓ ↓H␈↓16␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓Clearly␈α ␈↓↓last␈↓␈α computes␈αthe␈αlast␈αelement␈αof␈αa␈αlist.␈α ␈↓↓last ␈↓¬NIL␈↓↓␈↓␈αis␈αundefined␈αin␈αthe␈αLISP␈αlanguage;␈αthe
␈↓ ↓H␈↓result␈αof␈αtrying␈αto␈αcompute␈αit␈αmay␈αbe␈αan␈αerror␈αmessage␈αor␈αmay␈αbe␈αsome␈αrandom␈αresult␈αdepending
␈↓ ↓H␈↓on␈αthe␈αimplementation.␈α (A␈αheavy␈αduty␈αversion␈αof␈α␈↓↓last␈↓␈αwould␈αexplicitly␈αcall␈αa␈αfunction␈αcalled␈α␈↓↓error␈↓
␈↓ ↓H␈↓with a string expressing the complaint that the program had tried to compute ␈↓↓last␈↓ ␈↓¬NIL␈↓.)  

␈↓ ↓H␈↓        The function  ␈↓↓subst␈↓  is defined by

␈↓ ↓H␈↓8.3) ␈↓ α∂␈↓↓subst[x, y, z] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓z ␈↓αthen␈↓↓ [␈↓αif␈↓↓ z ␈↓αeq␈↓↓ y ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ z] ␈↓αelse␈↓↓ subst[x,y,␈↓αa|␈↓↓z] . subst[x,y,␈↓αd|␈↓↓z]␈↓. 

␈↓ ↓H␈↓We have

␈↓ ↓H␈↓␈↓ ↓x␈↓↓subst[␈↓¬(A.B), X, ((X.A).X)␈↓↓] ␈↓␈↓ ¬_␈↓↓= subst[␈↓¬(A.B), X, (X.A)␈↓↓] . subst[␈↓¬(A.B), X, X␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= [subst[␈↓¬(A.B), X, X␈↓↓] . subst[␈↓¬(A.B), X, A␈↓↓]] . ␈↓¬(A.B)␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= [[␈↓¬(A.B)␈↓↓].␈↓¬A␈↓↓].␈↓¬(A.B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬(((A.B).A).(A.B))␈↓↓␈↓.


␈↓ ↓H␈↓␈↓↓subst␈↓␈αcomputes␈αthe␈αresult␈αof␈αsubstituting␈αthe␈αS-expression␈α␈↓↓x␈↓␈αfor␈αthe␈αatom␈α␈↓↓y␈↓␈αin␈αthe␈αS-expression␈α␈↓↓z.␈↓
␈↓ ↓H␈↓This operation is important in all kinds of symbolic computation.

␈↓ ↓H␈↓        The␈α∩function␈α∩␈↓↓append[u,v]␈↓␈α∩which␈α∩gives␈α∩the␈α∪concatenation␈α∩of␈α∩the␈α∩lists␈α∩␈↓↓u␈↓␈α∩and␈α∩␈↓↓v␈↓␈α∪is␈α∩also
␈↓ ↓H␈↓important.  It is also denoted by the infixed expression  ␈↓↓u * v␈↓.  For example we have

␈↓ ↓H␈↓␈↓ ∧3␈↓↓␈↓¬  (A B C)␈↓↓ * ␈↓¬(D E F)␈↓↓ = ␈↓¬(A B C D E F)␈↓↓,␈↓

␈↓ ↓H␈↓␈↓ ∧S␈↓↓␈↓¬NIL␈↓↓ * ␈↓¬(A B)␈↓↓ =␈↓¬(A B)␈↓↓ = ␈↓¬(A B)␈↓↓ * ␈↓¬NIL␈↓↓,␈↓

␈↓ ↓H␈↓and the formal definition

␈↓ ↓H␈↓8.4) ␈↓ ∧C␈↓↓u * v ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u] * v␈↓. 

␈↓ ↓H␈↓The␈α∞␈↓↓append␈↓␈α∞function␈α
is␈α∞machine␈α∞coded␈α
in␈α∞most␈α∞Lisp␈α
systems␈α∞in␈α∞a␈α
way␈α∞that␈α∞allows␈α∞an␈α
arbitrary
␈↓ ↓H␈↓number␈α∃of␈α∀arguments,␈α∃e.g.␈α∃ ␈↓¬(APPEND␈α∀(QUOTE␈α∃(A␈α∃B))␈α∀(QUOTE␈α∃(C␈α∃D))␈α∀(QUOTE␈α∃(E␈α∃F)))␈↓␈α∀is
␈↓ ↓H␈↓␈↓¬(A B C D E F)␈↓.␈α
 It␈αis␈α
convenient␈α
to␈αuse␈α
an␈α
infix␈αfor␈α
␈↓↓append␈↓␈α
because␈αit␈α
is␈α
associative,␈αi.e.␈α
 ␈↓↓u␈α
*␈α[v␈α
*
␈↓ ↓H␈↓↓w]␈α∞=␈α∞[u␈α∞*␈α
v]␈α∞*␈α∞w␈↓␈α∞so␈α∞we␈α
can␈α∞just␈α∞write␈α∞ ␈↓↓u␈α
*␈α∞v␈α∞*␈α∞w␈↓.␈α∞We␈α
often␈α∞use␈α∞infix␈α∞notation␈α∞for␈α
recursively
␈↓ ↓H␈↓defined functions when it is mathematically conventional or convenient.

␈↓ ↓H␈↓        The␈αpropositional␈αoperations␈α
can␈αalso␈αbe␈α
used␈αin␈αmaking␈α
recursive␈αdefinitions␈αsince␈αwe␈α
take
␈↓ ↓H␈↓advantage of the order of evaluation of constituents.  Thus, we define a predicate  ␈↓↓equal␈↓  by

␈↓ ↓H␈↓8.5) ␈↓ α*␈↓↓equal[x,y] ← x ␈↓αeq␈↓↓ y ␈↓αor␈↓↓ [␈↓αnot␈↓↓ ␈↓αat|␈↓↓x ␈↓αand␈↓↓ ␈↓αnot␈↓↓ ␈↓αat|␈↓↓y ␈↓αand␈↓↓  equal[␈↓αa|␈↓↓x,␈↓αa|␈↓↓y] ␈↓αand␈↓↓ equal[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y]]␈↓. 

␈↓ ↓H␈↓␈↓↓equal[x,y]␈↓␈αis␈αtrue␈αif␈αand␈αonly␈αif␈α␈↓↓x␈↓␈αand␈α␈↓↓y␈↓␈αare␈αthe␈αsame␈αS-expression,␈αand␈αthe␈αuse␈αof␈αthis␈αpredicate
␈↓ ↓H␈↓makes␈αup␈αfor␈αthe␈αfact␈αthat␈αthe␈αbasic␈αpredicate␈α␈↓αeq␈↓␈αis␈αguaranteed␈αto␈αtest␈αequality␈αonly␈αwhen␈αone␈αof
␈↓ ↓H␈↓the operands is known to be an atom.  We shall also use the infixes  ␈↓α=␈↓  and  ␈↓α≠␈↓.

␈↓ ↓H␈↓        Membership of an S-expression ␈↓↓x␈↓ in a list ␈↓↓u␈↓ is tested by

␈↓ ↓H␈↓8.6) ␈↓ βU␈↓↓member[x, u] ← ␈↓αnot␈↓↓ ␈↓αn|␈↓↓u ␈↓αand␈↓↓ [[x = ␈↓αa|␈↓↓u] ␈↓αor␈↓↓ member[x, ␈↓αd|␈↓↓u]]␈↓. 

␈↓ ↓H␈↓This relation is also denoted by ␈↓↓x ε u␈↓.  Here are some computations:
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *17



␈↓ ↓H␈↓␈↓ βλ␈↓↓member[␈↓¬B, ␈↓↓␈↓¬(A B)␈↓↓]␈↓␈↓ ¬_␈↓↓= ␈↓αnot␈↓↓ ␈↓αn|␈↓↓␈↓¬(A B)␈↓↓ ␈↓αand␈↓↓ [[␈↓¬B ␈↓↓= ␈↓αa|␈↓↓␈↓¬(A B)␈↓↓]  ␈↓αor␈↓↓ member[␈↓¬B, ␈↓↓␈↓αd|␈↓↓␈↓¬(A B)␈↓↓]]␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= member[␈↓¬B, ␈↓↓␈↓¬(B)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓αtrue␈↓↓␈↓.


␈↓ ↓H␈↓        Sometimes␈α
a␈α
function␈α
is␈α
defined␈α
with␈α
the␈α
help␈α
of␈α
auxiliary␈α
functions.␈α
 Thus,␈α
the␈α
reverse␈α
of␈α
a
␈↓ ↓H␈↓list ␈↓↓u␈↓ , (e.g.  ␈↓↓reverse[␈↓¬(A B C D)␈↓↓] = ␈↓¬(D C B A)␈↓↓␈↓), is given by the pair of equations

␈↓ ↓H␈↓␈↓ ¬,␈↓↓reverse[u] ← rev[u, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓8.7)
␈↓ ↓H␈↓␈↓ ∧≥␈↓↓rev[u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ rev[␈↓αd|␈↓↓u, [␈↓αa|␈↓↓u].v]␈↓.

␈↓ ↓H␈↓A computation is:

␈↓ ↓H␈↓␈↓ β8␈↓↓reverse[␈↓¬(A B C)␈↓↓]␈↓␈↓ ¬_␈↓↓= rev[␈↓¬(A B C)␈↓↓, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= rev[␈↓¬(B C), (A)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= rev[␈↓¬(C), (B A)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= rev[␈↓¬NIL␈↓↓, ␈↓¬(C B A)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬(C B A)␈↓↓␈↓.


␈↓ ↓H␈↓A more elaborate example of recursive definition is given by

␈↓ ↓H␈↓␈↓ βh␈↓↓flatten[x] ← flat[x, ␈↓¬NIL␈↓↓]                                       ␈↓
␈↓ ↓H␈↓8.8)
␈↓ ↓H␈↓␈↓ βd␈↓↓flat[x, u] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x.u ␈↓αelse␈↓↓ flat[␈↓αa|␈↓↓x, flat[␈↓αd|␈↓↓x, u]]␈↓.

␈↓ ↓H␈↓We have

␈↓ ↓H␈↓␈↓ αx␈↓↓flatten[␈↓¬((A.B).C)␈↓↓]␈↓␈↓ ¬_␈↓↓= flat[␈↓¬((A.B).C)␈↓↓, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= flat[␈↓¬(A.B)␈↓↓, flat[␈↓¬C, ␈↓↓␈↓¬NIL␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= flat[␈↓¬(A.B), (C)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= flat[␈↓¬A, ␈↓↓flat[␈↓¬B, (C)␈↓↓]]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= flat[␈↓¬A, (B C)␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓= ␈↓¬(A B C)␈↓↓␈↓.


␈↓ ↓H␈↓The␈αreader␈αwill␈α
see␈αthat␈αthe␈αvalue␈α
of␈α␈↓↓flatten[x]␈↓␈αis␈αa␈α
list␈αof␈αthe␈αatoms␈α
of␈αthe␈αS-expression␈α
␈↓↓x␈↓␈αfrom
␈↓ ↓H␈↓left to right.  Thus ␈↓↓flatten[␈↓¬((A B) A)␈↓↓] = ␈↓¬(A B NIL A NIL)␈↓↓␈↓.

␈↓ ↓H␈↓        Many␈α⊃functions␈α⊃can␈α⊃be␈α∩conveniently␈α⊃written␈α⊃in␈α⊃more␈α∩than␈α⊃one␈α⊃way.␈α⊃ For␈α∩example,␈α⊃the
␈↓ ↓H␈↓function ␈↓↓reverse␈↓ mentioned above can be defined without an auxiliary function as follows:

␈↓ ↓H␈↓8.9) ␈↓ βn␈↓↓reverse1 u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ reverse1 ␈↓αd|␈↓↓u * <␈↓αa|␈↓↓u>␈↓, 

␈↓ ↓H␈↓but␈α⊃the␈α⊂earlier␈α⊃definition␈α⊂involves␈α⊃less␈α⊂computation,␈α⊃because␈α⊂*␈α⊃takes␈α⊂time␈α⊃proportional␈α⊃to␈α⊂the
␈↓ ↓H␈↓length␈αof␈αits␈αfirst␈αargument.␈α Similarly␈αthe␈αfunction␈αcomputed␈αby␈α␈↓↓flatten␈↓␈αcan␈αalso␈αbe␈αcomputed␈αby
␈↓ ↓H␈↓the simpler but less efficient program ␈↓↓fringe␈↓

␈↓ ↓H␈↓8.10) ␈↓ βi␈↓↓fringe x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <x> ␈↓αelse␈↓↓ fringe ␈↓αa|␈↓↓x * fringe ␈↓αd|␈↓↓x␈↓, 
␈↓ ↓H␈↓18␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓        The␈α∪use␈α∪of␈α∪conditional␈α∪expressions␈α∪for␈α∪recursive␈α∪function␈α∪definition␈α∪is␈α∪not␈α∀limited␈α∪to
␈↓ ↓H␈↓functions␈α
of␈α
S-expressions.␈α For␈α
example,␈α
the␈αfactorial␈α
function␈α
and␈αthe␈α
Euclidean␈α
algorithm␈αfor
␈↓ ↓H␈↓the greatest common divisor on the non-negative integers may be written as follows:

␈↓ ↓H␈↓8.11) ␈↓ ∧t␈↓↓n! ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ n␈↓π*␈↓↓[n-␈↓¬1␈↓↓]!␈↓ 

␈↓ ↓H␈↓and

␈↓ ↓H␈↓8.12) ␈↓ αO␈↓↓gcd[m, n] ← ␈↓αif␈↓↓ m>n ␈↓αthen␈↓↓ gcd[n, m] ␈↓αelse␈↓↓ ␈↓αif␈↓↓ m=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ n ␈↓αelse␈↓↓ gcd[n mod m, m]␈↓ 

␈↓ ↓H␈↓where␈α⊂␈↓↓n␈α⊂mod␈α⊂m␈↓␈α⊂denotes␈α⊂the␈α⊂remainder␈α⊂when␈α⊂␈↓↓n␈↓␈α⊂is␈α⊂divided␈α⊂by␈α⊂␈↓↓m␈↓␈α⊂and␈α⊂may␈α⊂itself␈α⊂be␈α⊂expressed
␈↓ ↓H␈↓recursively by

␈↓ ↓H␈↓8.13)␈↓ ∧)␈↓↓n mod m ← ␈↓αif␈↓↓ n<m ␈↓αthen␈↓↓ n ␈↓αelse␈↓↓ [n-m] mod m␈↓. 

␈↓ ↓H␈↓(Note␈α⊃that␈α⊃these␈α⊃definitions␈α⊃must␈α⊃be␈α⊃modified␈α⊃if␈α⊃negative␈α⊃integers␈α⊃are␈α⊃to␈α⊃be␈α⊃included␈α⊃in␈α⊃the
␈↓ ↓H␈↓domain.)

␈↓ ↓H␈↓        The␈αinternal␈αform␈αof␈αfunction␈αdefinitions␈αdepends␈αon␈αthe␈αimplementation.␈α MACLISP␈αuses
␈↓ ↓H␈↓the form

␈↓ ↓H␈↓␈↓ β-(␈↓¬DEFUN ␈↓<function name> <list of variables> <right hand side>).

␈↓ ↓H␈↓Stanford␈αLISP␈αand␈αUCI␈αLISP␈αfor␈αthe␈αPDP-10␈αcomputer␈αuse␈αthe␈αsame␈αform␈αwith␈α␈↓¬DE␈α␈↓␈αin␈αplace␈αof
␈↓ ↓H␈↓␈↓¬DEFUN. ␈↓ Thus in MACLISP the definition of ␈↓↓subst␈↓ is

␈↓ ↓H␈↓¬␈↓ α((DEFUN SUBST (X Y Z)
␈↓ ↓H␈↓¬␈↓ α(  (COND ((ATOM Z) (COND ((EQ Z Y) X) (T Z)))
␈↓ ↓H␈↓¬␈↓ α(        (T (CONS (SUBST X Y (CAR Z)) (SUBST X Y (CDR Z)))))),


␈↓ ↓H␈↓and the definition of ␈↓↓alt␈↓ is

␈↓ ↓H␈↓¬␈↓ αH(DEFUN ALT (U)
␈↓ ↓H␈↓¬␈↓ αH  (COND ((OR (NULL U) (NULL (CDR U))) U)
␈↓ ↓H␈↓¬␈↓ αH        (T (CONS (CAR U) (ALT (CDDR U)))))).


␈↓ ↓H␈↓Yet␈α
another␈α
notation␈α
for␈α
function␈α
definition␈αcalled␈α
the␈α
␈↓¬DEFPROP␈α
␈↓notation␈α
will␈α
be␈α
explained␈αafter
␈↓ ↓H␈↓λ-expressions have been introduced.


␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓        1. Consider the function ␈↓↓drop␈↓ defined by

␈↓ ↓H␈↓␈↓ ∧⊃␈↓↓drop[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ <␈↓αa|␈↓↓u> . drop[␈↓αd|␈↓↓u]␈↓. 

␈↓ ↓H␈↓Compute␈α⊃(by␈α⊃hand)␈α⊃␈↓↓drop[␈↓¬(A␈α⊃B␈α⊃C)␈↓↓]␈↓.␈α⊃ What␈α∩does␈α⊃␈↓↓drop␈↓␈α⊃do␈α⊃to␈α⊃lists␈α⊃in␈α⊃general?␈α⊃  Write␈α∩␈↓↓drop␈↓␈α⊃in
␈↓ ↓H␈↓internal notation using ␈↓¬DEFUN. ␈↓

␈↓ ↓H␈↓        2. What does the function
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *19


␈↓ ↓H␈↓␈↓ ∧∞␈↓↓r2[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ reverse[␈↓αa|␈↓↓u] . r2[␈↓αd|␈↓↓u]␈↓ 

␈↓ ↓H␈↓do to lists of lists?  How about

␈↓ ↓H␈↓␈↓ ∧∞␈↓↓r3[x] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ reverse[r4[x]]            ␈↓ 

␈↓ ↓H␈↓␈↓ ∧∩␈↓↓r4[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ r3[␈↓αa|␈↓↓u] . r4[␈↓αd|␈↓↓u]?    ␈↓ 

␈↓ ↓H␈↓        3. Compare

␈↓ ↓H␈↓␈↓ ∧ε␈↓↓r3'[x] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ <r3'[␈↓αd|␈↓↓x]> * <r3'[␈↓αa|␈↓↓x]>␈↓ 

␈↓ ↓H␈↓with the function ␈↓↓r3␈↓ of the preceding example.

␈↓ ↓H␈↓        4. Consider ␈↓↓r5␈↓ defined by

␈↓ ↓H␈↓␈↓ β∞␈↓↓r5[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αor␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ u  ␈↓αelse␈↓↓ [␈↓αa|␈↓↓r5[␈↓αd|␈↓↓u]] . r5[␈↓αa|␈↓↓u . r5[␈↓αd|␈↓↓r5[␈↓αd|␈↓↓u]]]␈↓. 

␈↓ ↓H␈↓Compute␈α␈↓↓r5[␈↓¬(A␈αB␈αC␈αD)␈↓↓]␈↓.␈α What␈αdoes␈α␈↓↓r5␈↓␈αdo?␈α  Needless␈αto␈αsay,␈αthis␈αis␈αnot␈αa␈αgood␈αway␈αof␈αcomputing
␈↓ ↓H␈↓this␈α⊂function␈α⊃even␈α⊂though␈α⊂it␈α⊃involves␈α⊂no␈α⊂auxiliary␈α⊃functions.␈α⊂ [This␈α⊂ingeneous␈α⊃definition␈α⊂was
␈↓ ↓H␈↓discovered by S. Ness]



␈↓ ↓H␈↓9.  ␈↓αNumerical computation.␈↓


␈↓ ↓H␈↓        Numerical␈α
calculation␈α
and␈α
symbolic␈α
calculation␈α
must␈α
often␈α
be␈α
combined,␈α
so␈α
LISP␈αprovides
␈↓ ↓H␈↓for␈α⊂numerical␈α⊂computation␈α⊂also.␈α⊂ (Numbers␈α⊂could␈α⊂be␈α⊂represented␈α⊂as␈α⊂lists␈α⊂of␈α⊂digits,␈α⊂but␈α⊂then␈α⊂it
␈↓ ↓H␈↓would␈α⊂be␈α⊂difficult␈α⊂to␈α⊂take␈α⊂proper␈α⊂advantage␈α⊂of␈α⊂machine␈α⊂instructions␈α⊂that␈α⊂work␈α⊂with␈α⊂numbers
␈↓ ↓H␈↓directly).␈α⊂ Since␈α⊂we␈α⊂need␈α⊂to␈α⊂include␈α⊂numbers␈α∂as␈α⊂parts␈α⊂of␈α⊂symbolic␈α⊂expressions,␈α⊂LISP␈α⊂has␈α∂both
␈↓ ↓H␈↓integer␈α
and␈α
floating␈αpoint␈α
numbers␈α
which␈α
are␈αregarded␈α
as␈α
atoms␈α
that␈αmay␈α
be␈α
included␈α
as␈αatoms␈α
in
␈↓ ↓H␈↓writing S-expressions.  Thus we have the lists:

␈↓ ↓H␈↓¬␈↓ ¬_(1 3 5)
␈↓ ↓H␈↓¬␈↓ ¬_(3.5 6.1 -7.2E9)
␈↓ ↓H␈↓¬␈↓ ¬_(PLUS X 1.3).


␈↓ ↓H␈↓The␈αfirst␈αis␈αa␈αlist␈αof␈α
integers,␈αthe␈αsecond␈αa␈αlist␈αof␈α
floating␈αpoint␈αnumbers,␈αand␈αthe␈αthird␈αa␈α
symbolic
␈↓ ↓H␈↓list␈α∂containing␈α∂both␈α∂numerical␈α∂and␈α∂non-numerical␈α∂atoms.␈α∂ Integers␈α∂are␈α∂written␈α⊂without␈α∂decimal
␈↓ ↓H␈↓points␈αwhich␈αare␈α
used␈αto␈αsignal␈α
floating␈αpoint␈αnumbers.␈α As␈α
in␈αFORTRAN,␈αthe␈α
letter␈αE␈αis␈αused␈α
to
␈↓ ↓H␈↓signal␈αthe␈αexponent␈αof␈αa␈αfloating␈αpoint␈αnumber␈αwhich␈αis␈αa␈αsigned␈αinteger.␈α The␈αsizes␈α
of␈αnumbers
␈↓ ↓H␈↓admitted␈αdepends␈αon␈αthe␈αimplementation.␈α When␈αa␈αdotted␈αpair,␈αsay␈α␈↓¬(1␈α.␈α2)␈↓␈αis␈αwanted,␈αthe␈αspaces
␈↓ ↓H␈↓around␈αthe␈αdot␈αdistinguish␈αit␈αfrom␈αthe␈αlist␈α␈↓¬(1.2)␈↓␈αwhose␈αsole␈αelement␈αis␈αthe␈αfloating␈αpoint␈αnumber
␈↓ ↓H␈↓1.2.

␈↓ ↓H␈↓        In␈α∞external␈α∞language␈α∞we␈α∞will␈α∞use␈α∞ordinary␈α∞mathematical␈α∞notation␈α∞for␈α∂numerical␈α∞functions.
␈↓ ↓H␈↓As␈α
an␈α
example␈α
of␈αa␈α
function␈α
combining␈α
numeric␈α
and␈αsymbolic␈α
calculation␈α
we␈α
have␈α
the␈αfunction
␈↓ ↓H␈↓giving the length of a list defined by
␈↓ ↓H␈↓20␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓9.1)␈↓ ∧.␈↓↓length u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ 0 ␈↓αelse␈↓↓ 1 + length ␈↓αd|␈↓↓u␈↓. 

␈↓ ↓H␈↓The internal notation for numerical functions is shown in the following examples:

␈↓ ↓H␈↓␈↓ ¬_␈↓¬(PLUS X Y ... Z)␈↓ for ␈↓↓x+y+...+z␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(TIMES X ... Z)␈↓ for ␈↓↓xy...z␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(MINUS X)␈↓ for ␈↓↓-x␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(DIFFERENCE X Y)␈↓ for ␈↓↓x-y␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(QUOTIENT X Y)␈↓ for ␈↓↓x/y␈↓,
␈↓ ↓H␈↓␈↓ ¬_␈↓¬(POWER X Y)␈↓ for ␈↓↓x␈↓∧y␈↓.


␈↓ ↓H␈↓        Besides␈α
functions␈αof␈α
numbers␈α
we␈αneed␈α
predicates␈α
on␈αnumbers␈α
and␈α
the␈αusual␈α
=,␈α
<,␈α>,␈α
≤,␈αand␈α
≥
␈↓ ↓H␈↓are␈α∀used␈α∪with␈α∀the␈α∀internal␈α∪names␈α∀␈↓¬EQUAL,␈α∀␈↓␈↓¬LESSP,␈α∪␈↓␈↓¬GREATERP,␈α∀␈↓␈↓¬LESSEQP,␈α∀␈↓and␈α∪␈↓¬GREATEREQP,
␈↓ ↓H␈↓¬␈↓respectively.␈α
 Not␈αall␈α
are␈α
implemented␈αin␈α
all␈αLISP␈α
systems,␈α
but␈αof␈α
course␈αthe␈α
remaining␈α
ones␈αcan
␈↓ ↓H␈↓be defined.  An additional predicate, ␈↓↓numberp,␈↓ is used to distinguish numbers from other atoms.

␈↓ ↓H␈↓        Since␈α∂numbers␈α∂that␈α∂form␈α∂part␈α∂of␈α∂list␈α∂structures␈α∂must␈α∂be␈α∂represented␈α∂by␈α∂pointers␈α∂anyway,
␈↓ ↓H␈↓there␈α⊃is␈α∩room␈α⊃for␈α∩a␈α⊃flag␈α∩distinguishing␈α⊃floating␈α∩point␈α⊃numbers␈α∩and␈α⊃integers.␈α∩ Therefore,␈α⊃the
␈↓ ↓H␈↓arithmetic␈α⊃operations␈α⊂are␈α⊃programmed␈α⊂to␈α⊃treat␈α⊂types␈α⊃dynamically,␈α⊂i.e.␈α⊃a␈α⊂variable␈α⊃may␈α⊃take␈α⊂an
␈↓ ↓H␈↓integer␈αvalue␈αat␈α
one␈αstep␈αof␈αcomputation␈α
and␈αa␈αreal␈αvalue␈α
at␈αanother.␈α The␈α
subroutines␈αrealizing
␈↓ ↓H␈↓the arithmetic functions make the appropriate tests and create results of appropriate types.

␈↓ ↓H␈↓        It␈α_is␈α→worth␈α_remarking␈α→that␈α_including␈α_type␈α→flags␈α_in␈α→numbers␈α_would␈α→benefit␈α_many
␈↓ ↓H␈↓programming languages besides LISP and would not cost much in either storage or hardware.

␈↓ ↓H␈↓        Dynamic␈α⊂typing␈α⊂of␈α∂variables␈α⊂is␈α⊂slow␈α∂compared␈α⊂to␈α⊂direct␈α∂use␈α⊂of␈α⊂the␈α⊂machine's␈α∂arithmetic
␈↓ ↓H␈↓instructions,␈α⊗so␈α⊗that␈α⊗LISP␈α∃can␈α⊗be␈α⊗efficiently␈α⊗used␈α∃interpretively␈α⊗only␈α⊗when␈α⊗the␈α∃numerical
␈↓ ↓H␈↓calculations␈α
are␈αsmall␈α
or␈α
at␈αleast␈α
small␈αcompared␈α
to␈α
the␈αsymbolic␈α
calculations␈αin␈α
a␈α
problem.␈α The
␈↓ ↓H␈↓MACLISP␈α∩compiler␈α⊃NCOMPLR␈α∩is␈α⊃able␈α∩to␈α⊃generate␈α∩efficient␈α⊃arithmetic␈α∩code.␈α∩ In␈α⊃particular,
␈↓ ↓H␈↓variables␈αcan␈α
be␈αdeclared␈αto␈α
be␈αof␈αa␈α
fixed␈αnumerical␈α
type␈αand␈αthe␈α
correct␈αmachine␈αinstructions␈α
are
␈↓ ↓H␈↓then␈α⊂generated␈α⊃so␈α⊂that␈α⊃runtime␈α⊂testing␈α⊂is␈α⊃not␈α⊂necessary.␈α⊃ Also,␈α⊂it␈α⊂uses␈α⊃separate␈α⊂stacks␈α⊃to␈α⊂store
␈↓ ↓H␈↓numerical␈αresults␈αso␈αthat␈αunecessary␈αconversion␈αfrom␈αthe␈αLISP␈αrepresentation␈αof␈αa␈αnumber␈αto␈αthe
␈↓ ↓H␈↓machine␈αrepresentation␈αcan␈αbe␈αavoided.␈α This␈αsaves␈αboth␈αtime␈αand␈αspace␈αas␈αeach␈αconversion␈αfrom
␈↓ ↓H␈↓a machine number to a LISP number requires a ␈↓↓cons␈↓ operation.

␈↓ ↓H␈↓        LISP␈α
can␈α
also␈α
deal␈α
with␈α
integers␈α
too␈α
large␈α
to␈α
be␈α
represented␈α
as␈α
a␈α
single␈α
machine␈α
word.␈α
 Such
␈↓ ↓H␈↓numbers␈α
are␈α∞called␈α
"bignums"␈α∞and␈α
are␈α∞repsented␈α
in␈α
LISP␈α∞by␈α
a␈α∞pointer␈α
to␈α∞a␈α
list␈α∞structure␈α
which
␈↓ ↓H␈↓contains␈αthe␈αsign,␈α
a␈αflag␈αsaying␈αthat␈α
this␈αa␈α"bignum",␈α
and␈αa␈αlist␈αof␈α
the␈αnumbers␈αcorresponding␈αto␈α
a
␈↓ ↓H␈↓base␈α∪B␈α∩representation␈α∪of␈α∩the␈α∪number␈α∩for␈α∪some␈α∩suitable␈α∪B␈α∩(depending␈α∪on␈α∩the␈α∪machine␈α∩and
␈↓ ↓H␈↓implementation).

␈↓ ↓H␈↓        As␈α∃another␈α∀example␈α∃of␈α∀a␈α∃combined␈α∀numeric␈α∃and␈α∀symbolic␈α∃computation,␈α∀we␈α∃give␈α∀an
␈↓ ↓H␈↓evaluator␈α⊂for␈α⊂expressions␈α⊂with␈α⊂sums␈α⊂and␈α⊃products.␈α⊂ The␈α⊂expressions␈α⊂are␈α⊂built␈α⊂up␈α⊃from␈α⊂atoms
␈↓ ↓H␈↓denoting variables and integer constants according to the syntax

␈↓ ↓H␈↓                <expression> ::= <variable> | <integer> | (␈↓¬PLUS ␈↓<explist>) | (␈↓¬TIMES ␈↓<explist>)
␈↓ ↓H␈↓9.2)
␈↓ ↓H␈↓                     <explist> ::= <expression> | <expression><explist>
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *21


␈↓ ↓H␈↓Thus␈αa␈αlist␈αof␈αexpressions␈αbeginning␈αwith␈αthe␈αatom␈α␈↓¬PLUS␈α␈↓represents␈αthe␈αsum␈αof␈αthese␈αexpressions
␈↓ ↓H␈↓and␈α∀a␈α∀list␈α∀of␈α∀expressions␈α∀beginning␈α∀with␈α∀the␈α∀atom␈α∀␈↓¬TIMES␈α∀␈↓represents␈α∀the␈α∀product␈α∃of␈α∀these
␈↓ ↓H␈↓expressions.␈α In␈αorder␈αto␈αevaluate␈αan␈αexpression␈α
we␈αneed␈αa␈αway␈αof␈αgiving␈αvalues␈αto␈α
the␈αvariables
␈↓ ↓H␈↓occuring␈α
in␈αthe␈α
expression.␈α  We␈α
will␈αuse␈α
an␈α␈↓↓association␈↓␈α
␈↓↓list␈↓␈αfor␈α
this␈αpurpose.␈α
 An␈α
association␈αlist
␈↓ ↓H␈↓(a-list)␈α
is␈α
a␈α∞list␈α
of␈α
pairs,␈α∞in␈α
our␈α
case␈α∞the␈α
first␈α
element␈α
of␈α∞each␈α
pair␈α
is␈α∞a␈α
variable␈α
and␈α∞the␈α
second
␈↓ ↓H␈↓element␈α
is␈α
the␈α
value␈α
associated␈αwith␈α
it.␈α
 For␈α
example␈α
 ␈↓¬((X␈α.␈α
5)␈α
(Y␈α
.␈α
9.3)␈α(Z␈α
.␈α
2.1))␈↓␈α
is␈α
an␈αa-
␈↓ ↓H␈↓list.␈α To␈αlook␈αup␈αthe␈αvalue␈αof␈αa␈αvariable␈αin␈αan␈αa-list␈αwe␈αuse␈αthe␈αfunction␈α␈↓↓assoc,␈↓␈αwhich␈αreturns␈αthe
␈↓ ↓H␈↓first␈α
pair␈α
in␈α
the␈α
list␈α
such␈α
that␈α
the␈α
variable␈α
matches␈α
the␈α
variable␈α
argument.␈α
 It␈α
returns␈α
␈↓¬NIL␈↓␈α
if␈αno
␈↓ ↓H␈↓value is found.  Thus

␈↓ ↓H␈↓␈↓ βG␈↓↓assoc[␈↓¬Y␈↓↓, ␈↓¬((X . 5) (Y . 9.3) (Z . 2.1))␈↓↓] = ␈↓¬(Y . 9.3)␈↓↓␈↓ 

␈↓ ↓H␈↓and the definition of ␈↓↓assoc␈↓ is given by

␈↓ ↓H␈↓9.3) ␈↓ α{␈↓↓assoc[x,a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓a ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓a ␈↓αeq␈↓↓ x ␈↓αthen␈↓↓ ␈↓αa|␈↓↓a ␈↓αelse␈↓↓ assoc[x,␈↓αd|␈↓↓a].␈↓ 

␈↓ ↓H␈↓Association␈αlists␈αare␈αgenerally␈αuseful␈αfor␈αassociating␈α"values"␈αwith␈α"symbols"␈αand␈αthey␈α
will␈αappear
␈↓ ↓H␈↓in␈α
many␈α∞later␈α
examples.␈α∞ We␈α
note␈α∞here␈α
two␈α∞features␈α
which␈α∞are␈α
due␈α∞to␈α
the␈α∞way␈α
␈↓↓assoc␈↓␈α∞is␈α
defined.
␈↓ ↓H␈↓Since␈α␈↓↓assoc␈↓␈αreturns␈αthe␈αpair␈αrather␈αthan␈αthe␈αvalue␈αwhen␈αthe␈αdesired␈αsymbol␈αis␈αfound␈αor␈α␈↓¬NIL␈↓␈αif␈αno
␈↓ ↓H␈↓value␈αis␈αfound,␈αthe␈αcase␈αof␈αno␈αvalue␈αfound␈αcan␈αbe␈αdetected.␈α If␈αjust␈αthe␈αvalue␈αwere␈αreturned␈αthere
␈↓ ↓H␈↓would␈α
be␈α
no␈α
distinction␈α
between␈αno␈α
value␈α
and␈α
a␈α
value␈α
of␈α␈↓¬NIL␈↓.␈α
 The␈α
calling␈α
program␈α
must␈αcheck␈α
to
␈↓ ↓H␈↓see␈α∂if␈α∂a␈α∂value␈α∞was␈α∂returned␈α∂(␈↓↓␈↓αnot␈↓↓␈α∂␈↓αn|␈↓↓assoc[x,a]␈↓)␈α∂and␈α∞then␈α∂extract␈α∂that␈α∂value␈α∂(␈↓↓␈↓αd|␈↓↓assoc[x,a]␈↓).␈α∞ Also,
␈↓ ↓H␈↓␈↓↓assoc␈↓␈αreturns␈αthe␈αfirst␈αpair␈αit␈αfinds,␈αthus␈αa␈αsymbol␈αcan␈αbe␈αassociated␈αwith␈αseveral␈αvalues␈αin␈αthe␈αa-
␈↓ ↓H␈↓list, but always the first occurence determines the result that is returned.   Thus

␈↓ ↓H␈↓␈↓ α1␈↓↓assoc[␈↓¬X␈↓↓,␈↓¬((U . 1.41) (X . 5) (Y . 9.3) (X . 1.2) (Z . 2.1))␈↓↓] = ␈↓¬(X . 5)␈↓↓␈↓. 

␈↓ ↓H␈↓␈↓↓assoc␈↓ is built into most LISP systems.

␈↓ ↓H␈↓        The interpreter, ␈↓↓numval,␈↓ can now be defined.

␈↓ ↓H␈↓␈↓ αx␈↓↓numval[e,a]␈↓␈↓ ∧8␈↓↓← ␈↓αif␈↓↓ numberp e ␈↓αthen␈↓↓ e␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ ␈↓αd|␈↓↓assoc[e,a]␈↓
␈↓ ↓H␈↓9.4)␈↓ ∧8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e ␈↓αeq␈↓↓ ␈↓¬PLUS ␈↓↓␈↓αthen␈↓↓ evplus[␈↓αd|␈↓↓e,a]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓  ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e ␈↓αeq␈↓↓ ␈↓¬TIMES ␈↓↓␈↓αthen␈↓↓ evtimes[␈↓αd|␈↓↓e,a]␈↓

␈↓ ↓H␈↓where

␈↓ ↓H␈↓9.5) ␈↓ αJ␈↓↓ evplus[u,a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ 0 ␈↓αelse␈↓↓ numval[␈↓αa|␈↓↓u,a] + evplus[␈↓αd|␈↓↓u,a],                  ␈↓

␈↓ ↓H␈↓9.6) ␈↓ αb␈↓↓evtimes[u,a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ numval[␈↓αa|␈↓↓u,a] ␈↓π*␈↓↓ evtimes[␈↓αd|␈↓↓u,a],             ␈↓





␈↓ ↓H␈↓10.  ␈↓αBitwise Boolean Operations␈↓


␈↓ ↓H␈↓        Besides␈αthe␈α
usual␈αarithmetic␈α
operations␈αon␈α
numbers,␈αLISP␈α
allows␈αbitwise␈αboolean␈α
operations
␈↓ ↓H␈↓22␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓on␈αwords␈αin␈αmemory.␈α Letting␈α␈↓↓m,␈↓␈αand␈α␈↓↓n␈↓␈αbe␈αstrings␈αof␈α␈↓¬1␈↓s␈αand␈α␈↓¬0␈↓s␈αof␈αsome␈αfixed␈α
length␈αdepending
␈↓ ↓H␈↓on␈αthe␈αmachine␈αword␈αsize,␈αthen␈α␈↓↓m␈α␈↓αbool-op␈↓↓␈αn␈↓␈αdenotes␈αthe␈αword␈αwhose␈αbinary␈αrepsentation␈αis␈αgiven
␈↓ ↓H␈↓by performing the boolean operation on each pair of corresponding bits.  Thus we have

␈↓ ↓H␈↓␈↓ ¬O␈↓¬4 ␈↓αbool-and␈↓¬ 6 = 4␈↓
␈↓ ↓H␈↓␈↓ ¬R␈↓¬ 4 ␈↓αbool-or␈↓¬ 6 = 6␈↓
␈↓ ↓H␈↓␈↓ ¬P␈↓¬4 ␈↓αbool-xor␈↓¬ 6 = 2␈↓

␈↓ ↓H␈↓Here␈α⊂␈↓αbool-xor␈↓␈α⊂is␈α⊃the␈α⊂exclusive␈α⊂or␈α⊃operation.␈α⊂ In␈α⊂general␈α⊂we␈α⊃will␈α⊂use␈α⊂terminology␈α⊃obtained␈α⊂by
␈↓ ↓H␈↓prefixing ␈↓αbool-␈↓ to the usual name of the corresponding logical operation for a boolean operation.

␈↓ ↓H␈↓        The␈α
internal␈α
form␈αfor␈α
boolean␈α
operations␈αon␈α
words␈α
has␈α
not␈αbeen␈α
standardized,␈α
but␈αthe␈α
form
␈↓ ↓H␈↓used by MACLISP is

␈↓ ↓H␈↓␈↓ ¬_(␈↓¬BOOLE ␈↓<code> <n1> <n2>)

␈↓ ↓H␈↓where␈α<code>␈αranges␈αfrom␈α␈↓¬0␈α␈↓to␈α␈↓¬15␈α␈↓(decimal)␈αand␈αeach␈αvalue␈αof␈α<code>␈αcorresponds␈αto␈αa␈αdifferent
␈↓ ↓H␈↓boolean␈α
operation.␈αWe␈α
have␈α
the␈αfollowing␈α
simple␈α
rule␈αfor␈α
determining␈α
what␈αoperation␈α
corresponds
␈↓ ↓H␈↓to␈α
a␈α
particular␈α
value␈α
of␈α
<code>.␈α
 If␈α
the␈αbinary␈α
representation␈α
of␈α
<code>␈α
is␈α
"␈↓¬abcd␈↓"␈α
then␈α
result␈αof␈α
the
␈↓ ↓H␈↓operation on a pair of bits ␈↓¬x ␈↓and ␈↓¬y ␈↓is given in table 3.

␈↓ ↓H␈↓¬␈↓ ∧8x␈↓ ∧xy␈↓ ¬x(BOOLE "abcd" x y)

␈↓ ↓H␈↓¬␈↓ ∧80␈↓ ∧x0␈↓ π_a
␈↓ ↓H␈↓¬␈↓ ∧80␈↓ ∧x1␈↓ π_c
␈↓ ↓H␈↓¬␈↓ ∧81␈↓ ∧x0␈↓ π_b
␈↓ ↓H␈↓¬␈↓ ∧81␈↓ ∧x1␈↓ π_d



␈↓ ↓H␈↓¬␈↓ ∧J␈↓αTable 3.␈↓¬  ␈↓Boolean operation encoding.␈↓¬


␈↓ ↓H␈↓In␈α∂particular␈α∂if␈α∂<code>␈α∂is␈α∂␈↓¬0␈α∂␈↓we␈α∂have␈α∂the␈α∂identically␈α∂␈↓¬0␈α∂␈↓operation,␈α∂if␈α∂<code>␈α∂is␈α∂␈↓¬15␈α∂␈↓we␈α⊂have␈α∂the
␈↓ ↓H␈↓identically␈α∞␈↓¬1␈α∞␈↓operation,␈α∞if␈α
<code>␈α∞is␈α∞␈↓¬1␈α∞␈↓we␈α
have␈α∞ ␈↓αbool-and␈↓,␈α∞and␈α∞if␈α
<code>␈α∞is␈α∞␈↓¬7␈α∞␈↓we␈α∞have␈α
 ␈↓αbool-or␈↓.
␈↓ ↓H␈↓Note that all 16 boolean functions of two variables are realized by the LISP ␈↓¬BOOLE ␈↓operation.

␈↓ ↓H␈↓        In␈α∞addition␈α
to␈α∞the␈α
above␈α∞operations␈α
LISP␈α∞has␈α
a␈α∞shift␈α
operation␈α∞␈↓↓lsh[n,d]␈↓␈α
which␈α∞shifts␈α
the
␈↓ ↓H␈↓bits␈α⊂of␈α⊂the␈α∂binary␈α⊂representation␈α⊂of␈α∂␈↓↓n,␈↓␈α⊂␈↓↓d␈↓␈α⊂positions␈α∂to␈α⊂the␈α⊂left,␈α∂filling␈α⊂the␈α⊂vacated␈α⊂spaces␈α∂with
␈↓ ↓H␈↓zeroes.␈α If␈α␈↓↓d␈↓␈αis␈αnegative␈αthe␈αshifting␈αis␈αto␈αthe␈αright␈αinstead.␈α The␈αinternal␈αform␈αof␈αthis␈αoperation␈α
is
␈↓ ↓H␈↓(␈↓¬LSH␈↓ <n> <d>).

␈↓ ↓H␈↓        Boolean␈α∞operations␈α∞are␈α∞particularly␈α∞useful␈α∞when␈α∞you␈α∞have␈α∞a␈α∞problem␈α∞involving␈α∞data␈α
that
␈↓ ↓H␈↓can␈α∞easily␈α∞be␈α
represented␈α∞by␈α∞bit␈α∞strings␈α
or␈α∞vectors.␈α∞ Using␈α∞the␈α
Boolean␈α∞operations␈α∞is␈α∞very␈α
much
␈↓ ↓H␈↓more␈α⊂efficient␈α⊃than␈α⊂representing␈α⊃the␈α⊂same␈α⊂data␈α⊃as␈α⊂lists␈α⊃of␈α⊂␈↓¬0␈↓s␈α⊂and␈α⊃␈↓¬1␈↓s,␈α⊂both␈α⊃in␈α⊂terms␈α⊃of␈α⊂space
␈↓ ↓H␈↓required␈α⊂and␈α⊂in␈α⊂terms␈α⊂of␈α∂computation␈α⊂time.␈α⊂ It␈α⊂does␈α⊂not␈α∂necessarily␈α⊂make␈α⊂the␈α⊂problem␈α⊂or␈α∂the
␈↓ ↓H␈↓programs␈α
easier␈α
to␈αunderstand,␈α
however.␈α
  We␈α
will␈αsee␈α
an␈α
example␈α
of␈αa␈α
problem␈α
solved␈αusing␈α
both
␈↓ ↓H␈↓list and bit represtations in Chapter .
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *23


␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓        1. What boolean operation does ␈↓¬BOOLE ␈↓perform when <code>=␈↓¬11 ␈↓(decimal)?

␈↓ ↓H␈↓        2. What <code> corresponds to the operation ␈↓¬x=y␈↓ ?



␈↓ ↓H␈↓11.  ␈↓αLambda expressions and functions with functions as arguments.␈↓


␈↓ ↓H␈↓        It␈αis␈αcommon␈αto␈α
use␈αphrases␈αlike␈α"the␈α
function␈α␈↓↓2x+y␈↓".␈α This␈αis␈α
not␈αa␈αprecise␈αnotation␈α
because
␈↓ ↓H␈↓we␈αcannot␈αsay␈α␈↓↓[2x+y][3, 4]␈↓␈αand␈αknow␈αwhether␈α
the␈αdesired␈αresult␈αis␈α2␈↓π*␈↓3+4␈αor␈α2␈↓π*␈↓4+3␈α
regarding␈αthe
␈↓ ↓H␈↓expression␈α⊃as␈α⊃a␈α⊃function␈α⊃of␈α⊃two␈α⊃variables.␈α⊃ Worse␈α⊃yet,␈α⊃we␈α⊃might␈α⊃have␈α⊃meant␈α∩a␈α⊃one-variable
␈↓ ↓H␈↓function of ␈↓↓x␈↓ wherein ␈↓↓y␈↓ is regarded as a parameter.

␈↓ ↓H␈↓        The␈αproblem␈αof␈αgiving␈αnames␈αto␈αfunctions␈αis␈αsolved␈αby␈αChurch's␈αλ-notation.␈α In␈αthe␈αabove
␈↓ ↓H␈↓example,␈αwe␈αwould␈αwrite␈α␈↓↓λx y: 2x+y␈↓␈αto␈αdenote␈αthe␈αfunction␈αof␈αtwo␈αvariables␈αwith␈αfirst␈αargument␈α␈↓↓x␈↓
␈↓ ↓H␈↓and␈α#second␈α"argument␈α#␈↓↓y␈↓␈α"whose␈α#value␈α#is␈α"given␈α#by␈α"the␈α#expression␈α#␈↓↓2x+y␈↓.␈α" Thus,
␈↓ ↓H␈↓␈↓↓[λx y: 2x+y][3, 4] = 10␈↓ and  ␈↓↓[λy x: 2x+y][3, 4]= 11.␈↓

␈↓ ↓H␈↓        Like␈α⊂variables␈α⊃of␈α⊂integration␈α⊂and␈α⊃the␈α⊂bound␈α⊂variables␈α⊃of␈α⊂quantifiers␈α⊂in␈α⊃logic,␈α⊂variables
␈↓ ↓H␈↓following␈α∪the␈α∪ λ ␈α∪are␈α∀bound␈α∪or␈α∪dummy␈α∪and␈α∪may␈α∀be␈α∪replaced␈α∪by␈α∪any␈α∪others␈α∀provided␈α∪the
␈↓ ↓H␈↓replacement␈α
is␈α
done␈α
consistently␈α
throughout␈α
the␈α
expression␈α
and␈α
does␈α
not␈α
make␈α
any␈αvariable␈α
bound
␈↓ ↓H␈↓by␈α
 λ ␈α
the␈α
same␈α
as␈α
a␈α
free␈α
variable␈α
in␈α
the␈α
expression.␈α
 Thus␈α
␈↓↓λx y: 2x+y␈↓␈α
represents␈α
the␈α
same␈α
function
␈↓ ↓H␈↓as␈α␈↓↓λy x: 2y+x␈↓␈αor␈α␈↓↓λu v: 2u+v␈↓␈α,␈αbut␈αin␈αthe␈αfunction␈αof␈αone␈αargument␈α␈↓↓λx: 2x+y␈↓,␈αwe␈αcannot␈αreplace␈αthe
␈↓ ↓H␈↓variable ␈↓↓x␈↓ by ␈↓↓y,␈↓ though we could replace it by ␈↓↓u.␈↓

␈↓ ↓H␈↓        λ-notation␈α∞plays␈α
two␈α∞important␈α
roles␈α∞in␈α
LISP.␈α∞ First,␈α
it␈α∞allows␈α
us␈α∞to␈α
rewrite␈α∞an␈α
expression
␈↓ ↓H␈↓containing␈αtwo␈αor␈αmore␈αoccurrences␈αof␈αthe␈αsame␈αsub-expression␈αin␈αsuch␈αa␈αway␈αthat␈αthe␈αexpression
␈↓ ↓H␈↓occurs␈α∪only␈α∩once.␈α∪Thus␈α∩␈↓↓(2x+1)␈↓∧4␈↓↓+3(2x+1)␈↓∧3␈↓␈α∪can␈α∩be␈α∪written␈α∩␈↓↓[λw: w␈↓∧4␈↓↓+3w␈↓∧3␈↓↓][2x+1]␈↓.␈α∪ This␈α∪can␈α∩save
␈↓ ↓H␈↓considerable␈αcomputation,␈α
and␈αcorresponds␈αto␈α
the␈αpractice␈αin␈α
sequential␈αprogramming␈αof␈α
assigning
␈↓ ↓H␈↓to␈αa␈α
variable␈αthe␈αvalue␈α
of␈αa␈αsub-expression␈α
that␈αoccurs␈αmore␈α
than␈αonce␈αin␈α
an␈αexpression␈αand␈α
then
␈↓ ↓H␈↓writing the expression in terms of the variable.  It is sometimes called λ-binding.

␈↓ ↓H␈↓        The␈α∂second␈α∂use␈α∂of␈α⊂λ-expressions␈α∂is␈α∂in␈α∂using␈α⊂functions␈α∂that␈α∂take␈α∂functions␈α⊂as␈α∂arguments.
␈↓ ↓H␈↓Suppose␈αwe␈αwant␈αto␈αform␈αa␈αnew␈αlist␈αfrom␈αan␈αold␈αone␈αby␈αapplying␈αa␈αfunction␈α␈↓↓f␈↓␈αto␈αeach␈αelement␈αof
␈↓ ↓H␈↓the list.  This can be done using the function ␈↓↓mapcar␈↓ defined by

␈↓ ↓H␈↓11.1) ␈↓ βG␈↓↓mapcar[f, u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ f[␈↓αa|␈↓↓u] . mapcar[f, ␈↓αd|␈↓↓u]␈↓. 

␈↓ ↓H␈↓Such␈αa␈αfunction␈αis␈αcalled␈αa␈α␈↓↓functional␈↓␈αsince␈αone␈α(or␈αmore)␈αof␈αits␈αarguments␈αis␈αa␈αfunction.␈α ␈↓↓mapcar␈↓
␈↓ ↓H␈↓maps a function and a list into a list.  Some functionals map functions to functions.

␈↓ ↓H␈↓Suppose␈α⊃the␈α⊃operation␈α⊃we␈α⊃want␈α⊃to␈α⊃perform␈α⊃is␈α⊃squaring,␈α⊃and␈α⊃we␈α⊃want␈α⊃to␈α⊃apply␈α⊃it␈α⊃to␈α⊃the␈α⊃list
␈↓ ↓H␈↓␈↓¬(1 2 3 4 5 6 7)␈↓.  We have

␈↓ ↓H␈↓␈↓ β7␈↓↓mapcar[λx: x␈↓∧2␈↓↓, ␈↓¬(1 2 3 4 5 6 7)␈↓↓] = ␈↓¬(1 4 9 16 25 36 49)␈↓↓␈↓. 

␈↓ ↓H␈↓        [Some␈α∞implementations␈α∞of␈α∞LISP␈α∞allow␈α∞mapping␈α∞functions␈α∞to␈α∞take␈α∞an␈α∞arbitrary␈α∞number␈α∞of
␈↓ ↓H␈↓24␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓lists␈α
as␈α
arguments.␈α
 The␈α∞number␈α
of␈α
lists␈α
is␈α
the␈α∞number␈α
of␈α
arguments␈α
expected␈α
by␈α∞the␈α
functional
␈↓ ↓H␈↓argument␈αand␈αthe␈αmapping␈αterminates␈αwhen␈αthe␈αshortest␈αlist␈αis␈αexhausted.␈α Some␈αimplementations
␈↓ ↓H␈↓of LISP require the arguments in reverse order - functional argument second.]

␈↓ ↓H␈↓        A␈αmore␈αgenerally␈αuseful␈αoperation␈αthan␈α␈↓↓mapcar␈↓␈αis␈α␈↓↓maplist␈↓␈αin␈αwhich␈αthe␈αfunction␈αis␈αapplied
␈↓ ↓H␈↓to the successive sublists of the list rather than to the elements.  ␈↓↓maplist␈↓ is defined by

␈↓ ↓H␈↓11.2) ␈↓ βN␈↓↓maplist[f, u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ f[u] . maplist[f, ␈↓αd|␈↓↓u]␈↓. 

␈↓ ↓H␈↓        As␈α⊃an␈α⊃application␈α⊂of␈α⊃␈↓↓maplist␈↓␈α⊃and␈α⊂functional␈α⊃arguments,␈α⊃we␈α⊂shall␈α⊃define␈α⊃a␈α⊃function␈α⊂for
␈↓ ↓H␈↓differentiating␈α_algebraic␈α_expressions␈α↔involving␈α_sums␈α_and␈α↔products.␈α_ The␈α_syntax␈α_for␈α↔such
␈↓ ↓H␈↓expressions␈α
was␈α
given␈α
in␈α
(9.2).␈α
 Recall,␈α
␈↓¬PLUS␈α
␈↓followed␈α
by␈α
a␈α
list␈α
of␈α
arguments␈α
denotes␈α
the␈α∞sum␈α
of
␈↓ ↓H␈↓these␈αarguments␈αand␈α
␈↓¬TIMES␈α␈↓followed␈αby␈α
a␈αlist␈αof␈αarguments␈α
denotes␈αtheir␈αproduct.␈α
 The␈αfunction
␈↓ ↓H␈↓␈↓↓diff[e, v]␈↓ gives the partial derivative of the expression ␈↓↓e␈↓ with respect to the variable ␈↓↓v.␈↓ We have


␈↓ ↓H␈↓␈↓ αx␈↓↓diff[e, v] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ [␈↓αif␈↓↓ e = v ␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ 0]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬PLUS␈↓↓ ␈↓αthen␈↓↓ ␈↓¬PLUS␈↓↓ . mapcar[[λx: diff[x, v]], ␈↓αd|␈↓↓e]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬TIMES␈↓↓ ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓11.3)␈↓ βX␈↓↓␈↓¬PLUS␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓. maplist[␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓[λx: ␈↓¬TIMES␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓↓. maplist[␈↓
␈↓ ↓H␈↓␈↓ ¬λ␈↓↓[λy: ␈↓αif␈↓↓ x = y ␈↓αthen␈↓↓ diff[␈↓αa|␈↓↓y, v] ␈↓αelse␈↓↓ ␈↓αa|␈↓↓y], ␈↓αd|␈↓↓e]], ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αd|␈↓↓e]␈↓


␈↓ ↓H␈↓The term that describes the rule for differentiating products corresponds to the rule

␈↓ ↓H␈↓␈↓ ∧&␈↓↓∂/∂v[␈↓πP␈↓↓␈↓βi␈↓↓ e␈↓βi␈↓↓] = ␈↓πS␈↓↓␈↓βi␈↓↓␈↓π P␈↓↓␈↓βj␈↓↓ [␈↓αif␈↓↓ i=j ␈↓αthen␈↓↓ ∂e␈↓βj␈↓↓/∂v ␈↓αelse␈↓↓ e␈↓βj␈↓↓] .␈↓ 

␈↓ ↓H␈↓and␈α∂␈↓↓maplist␈↓␈α⊂has␈α∂to␈α⊂be␈α∂used␈α⊂rather␈α∂than␈α⊂␈↓↓mapcar␈↓␈α∂since␈α⊂whether␈α∂to␈α⊂differentiate␈α∂in␈α⊂forming␈α∂the
␈↓ ↓H␈↓product is determined by equality of the indices ␈↓↓i␈↓ and ␈↓↓j␈↓ rather than equality of the terms ␈↓↓e␈↓βi␈↓ and ␈↓↓e␈↓βj␈↓.

␈↓ ↓H␈↓        The internal form for a λ-expression is

␈↓ ↓H␈↓␈↓ βO(␈↓¬LAMBDA ␈↓<list of variables> <expression to be evaluated>).

␈↓ ↓H␈↓Thus␈α
␈↓↓λx:␈α
diff[x,v]␈↓␈α
is␈α
written␈α
␈↓¬(LAMBDA␈α
(X)␈α
(DIFF␈α
X␈α
V))␈↓.␈α
 The␈α
internal␈α
form␈α
of␈α
of␈α
␈↓↓diff␈↓␈α
is␈α
given
␈↓ ↓H␈↓below.  Notice that the function arguments to ␈↓↓maplist␈↓ and ␈↓↓mapcar␈↓ have the form

␈↓ ↓H␈↓␈↓ ¬(␈↓¬FUNCTION ␈↓ <λ-expression>).

␈↓ ↓H␈↓This␈αis␈α
necessary␈αif␈α
the␈αdefinition␈α
is␈αto␈α
be␈αcompiled␈α
as␈αthe␈α
compiler␈αmust␈α
recognize␈αthe␈α
fact␈αthat
␈↓ ↓H␈↓the␈αfollowing␈α
code␈αmust␈α
be␈αcompiled␈αas␈α
a␈αfunction.␈α
 If␈αthe␈αdefinition␈α
is␈αonly␈α
to␈αbe␈αinterpreted␈α
then
␈↓ ↓H␈↓␈↓¬FUNCTION␈α␈↓␈αhas␈αthe␈αsame␈αeffect␈αas␈α␈↓¬QUOTE␈α␈↓and␈αin␈αfact␈αyou␈αmay␈αuse␈α␈↓¬QUOTE␈α␈↓␈αinstead␈αof␈α␈↓¬FUNCTION␈α␈↓in
␈↓ ↓H␈↓definitions that will only be interpreted by MACLISP.
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *25



␈↓ ↓H␈↓¬(DEFUN DIFF (E V)
␈↓ ↓H␈↓¬  (COND ((ATOM E) (COND ((EQ E V) 1) (T 0)))
␈↓ ↓H␈↓¬        ((EQ (CAR E) 'PLUS)
␈↓ ↓H␈↓¬         (CONS 'PLUS
␈↓ ↓H␈↓¬               (MAPCAR (FUNCTION (LAMBDA (X) (DIFF X V))) (CDR E))))
␈↓ ↓H␈↓¬        ((EQ (CAR E) 'TIMES)
␈↓ ↓H␈↓¬         (CONS 'PLUS
␈↓ ↓H␈↓¬               (MAPLIST (FUNCTION
␈↓ ↓H␈↓¬                          (LAMBDA (X)
␈↓ ↓H␈↓¬                            (CONS 'TIMES
␈↓ ↓H␈↓¬                                  (MAPLIST (FUNCTION
␈↓ ↓H␈↓¬                                            (LAMBDA (Y)
␈↓ ↓H␈↓¬                                              (COND ((EQ X Y) (DIFF (CAR Y) V))
␈↓ ↓H␈↓¬                                                    (T (CAR Y)))))
␈↓ ↓H␈↓¬                                           (CDR E)))))
␈↓ ↓H␈↓¬                        (CDR E))))))


␈↓ ↓H␈↓        The␈α
above␈α
paragraphing␈α
(known␈α
as␈α"pretty␈α
printing")␈α
makes␈α
function␈α
definitions␈α
easier␈αto
␈↓ ↓H␈↓read because items beginning in the same column are at the same parenthetical level.

␈↓ ↓H␈↓        Two␈α∩additional␈α⊃useful␈α∩functions␈α⊃with␈α∩functions␈α⊃as␈α∩arguments␈α⊃are␈α∩the␈α⊃predicates  ␈↓↓andlis␈↓
␈↓ ↓H␈↓ and  ␈↓↓orlis␈↓  defined by the equations


␈↓ ↓H␈↓11.4) ␈↓ ∧∞␈↓↓andlis[p, u] ← ␈↓αn|␈↓↓u ␈↓αor␈↓↓ [p[␈↓αa|␈↓↓u] ␈↓αand␈↓↓ andlis[p, ␈↓αd|␈↓↓u]]␈↓

␈↓ ↓H␈↓11.5) ␈↓ ∧β␈↓↓ orlis[p, u] ← ␈↓αnot␈↓↓ ␈↓αn|␈↓↓u ␈↓αand␈↓↓ [p[␈↓αa|␈↓↓u] ␈↓αor␈↓↓ orlis[p, ␈↓αd|␈↓↓u]]␈↓.


␈↓ ↓H␈↓        Another␈α∂way␈α∂of␈α∂writing␈α∂function␈α∂definitions␈α∞in␈α∂internal␈α∂notation␈α∂uses␈α∂␈↓¬LAMBDA␈α∂␈↓to␈α∂make␈α∞a
␈↓ ↓H␈↓function of the right side of a definition.  It is like writing ␈↓↓subst␈↓ and ␈↓↓alt␈↓ as


␈↓ ↓H␈↓␈↓ α∪␈↓↓subst ← λx y z:[␈↓αif␈↓↓ ␈↓αat|␈↓↓z ␈↓αthen␈↓↓ [␈↓αif␈↓↓ z ␈↓αeq␈↓↓ y ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ z]  ␈↓αelse␈↓↓ subst[x,y,␈↓αa|␈↓↓z] . subst[x,y,␈↓αd|␈↓↓z]]␈↓

␈↓ ↓H␈↓␈↓ ∧α␈↓↓alt ← λu.[␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αor␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . alt ␈↓αdd|␈↓↓u]␈↓.


␈↓ ↓H␈↓Internally these definitions of ␈↓↓subst␈↓ and ␈↓↓alt␈↓ take the forms

␈↓ ↓H␈↓¬␈↓ α((DEFPROP SUBST
␈↓ ↓H␈↓¬␈↓ α(  (LAMBDA (X Y Z)
␈↓ ↓H␈↓¬␈↓ α(    (COND ((ATOM Z) (COND ((EQ Z X) Y) (T Z)))
␈↓ ↓H␈↓¬␈↓ α(          (T (CONS (SUBST X Y (CAR Z)) (SUBST X Y (CDR Z))))))
␈↓ ↓H␈↓¬␈↓ α( EXPR)


␈↓ ↓H␈↓¬␈↓ α((DEFPROP ALT
␈↓ ↓H␈↓¬␈↓ α(  (LAMBDA (U) (COND ((OR (NULL U) (NULL (CDR U))) U)
␈↓ ↓H␈↓¬␈↓ α(                    (T (CONS (CAR U) (ALT (CDDR U))))))
␈↓ ↓H␈↓¬␈↓ α( EXPR).
␈↓ ↓H␈↓26␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓        The general form for this manner of writing functon definitions is

␈↓ ↓H␈↓␈↓ β?(␈↓¬DEFPROP ␈↓<function name> <defining λ-expression> ␈↓¬EXPR) ␈↓

␈↓ ↓H␈↓and␈α⊂it␈α⊃is␈α⊂often␈α⊃used␈α⊂by␈α⊃programs␈α⊂that␈α⊃output␈α⊂LISP.␈α⊂ It␈α⊃is␈α⊂a␈α⊃special␈α⊂case␈α⊃of␈α⊂an␈α⊃operation␈α⊂on
␈↓ ↓H␈↓property␈α
lists.␈α It␈α
puts␈α
the␈αλ-expression␈α
on␈α
the␈α␈↓¬EXPR␈α
␈↓property␈αof␈α
the␈α
function␈αname␈α
(which␈α
is␈αan
␈↓ ↓H␈↓atom).␈α
 ␈↓¬EXPR␈α␈↓says␈α
that␈α
the␈αitem␈α
is␈α
a␈αLISP␈α
function␈α
defined␈αby␈α
an␈α
S-expression.␈α (Rather␈α
than␈αby␈α
a
␈↓ ↓H␈↓machine␈αlanguage␈αsubroutine,␈αfor␈αinstance).␈α The␈α␈↓¬DEFUN␈α␈↓form␈αof␈αfunction␈αdefinition␈αhas␈αthe␈αsame
␈↓ ↓H␈↓effect␈αin␈αthat␈α
it␈αforms␈αa␈αλ-expression␈α
out␈αof␈αthe␈αlist␈α
of␈αvariables␈αand␈αthe␈α
right␈αhand␈αside␈αand␈α
puts
␈↓ ↓H␈↓it on the ␈↓¬EXPR ␈↓property of the function name.


␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓        1.␈α∂Compute␈α∞␈↓↓diff[␈↓¬(TIMES␈α∂X␈α∂(PLUS␈α∞Y␈α∂1)␈α∞3),␈α∂X␈↓↓]␈↓␈α∂using␈α∞the␈α∂above␈α∞definition␈α∂of␈α∂␈↓↓diff.␈↓␈α∞ Now
␈↓ ↓H␈↓do you see why algebraic simplification is important?

␈↓ ↓H␈↓        2. Compute ␈↓↓orlis[␈↓αat␈↓↓, ␈↓¬((A B) (C D) E)␈↓↓]␈↓.



␈↓ ↓H␈↓12.  ␈↓αLabel.␈↓


␈↓ ↓H␈↓        The␈αλ␈α
mechanism␈αis␈α
not␈αadequate␈α
for␈αproviding␈α
names␈αfor␈α
recursive␈αfunctions,␈α
because␈αin
␈↓ ↓H␈↓this␈αcase␈αthere␈αhas␈αto␈αbe␈αa␈αway␈αof␈αreferring␈αto␈αthe␈αfunction␈αname␈αwithin␈αthe␈αfunction.␈α Therefore,
␈↓ ↓H␈↓we␈α∞use␈α∞the␈α∞notation  ␈↓↓label[f, e]␈↓  to␈α∞denote␈α∞the␈α∞expression␈α∞␈↓↓e␈↓␈α∞but␈α∞where␈α∞occurrences␈α∞of␈α∞␈↓↓f␈↓␈α∂within␈α∞␈↓↓e␈↓
␈↓ ↓H␈↓refer␈α∞to␈α∞the␈α
whole␈α∞expression.␈α∞ For␈α∞example,␈α
suppose␈α∞we␈α∞wished␈α∞to␈α
define␈α∞a␈α∞function␈α∞that␈α
takes
␈↓ ↓H␈↓alternate elements of each element of a list and makes a list of these.  Thus, we want

␈↓ ↓H␈↓␈↓ β→␈↓↓altlis[␈↓¬((A B C) (A B C D) (X Y Z))␈↓↓] = ␈↓¬((A C) (A C) (X Z))␈↓↓␈↓. 

␈↓ ↓H␈↓We can make the definition

␈↓ ↓H␈↓12.1) ␈↓ αP␈↓↓altlis[u] ← mapcar[label[alt, λu: ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αor␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ u ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . alt[␈↓αdd|␈↓↓u]], u]␈↓. 

␈↓ ↓H␈↓in internal form this would be written

␈↓ ↓H␈↓¬␈↓ α((DEFUN ALTLIS (X)
␈↓ ↓H␈↓¬␈↓ α(  (MAPCAR (QUOTE (LABEL ALT
␈↓ ↓H␈↓¬␈↓ α(                   (LAMBDA (X)
␈↓ ↓H␈↓¬␈↓ α(                     (COND (OR (NULL X) (NULL (CDR X))) X)
␈↓ ↓H␈↓¬␈↓ α(                           (T (CONS (CAR X) (ALT (CDDR X)))))))
␈↓ ↓H␈↓¬␈↓ α(           X)).

␈↓ ↓H␈↓        The general internal form of the label construct is

␈↓ ↓H␈↓␈↓ ∧F(␈↓¬LABEL ␈↓<name> <function expression>),

␈↓ ↓H␈↓        The␈α
identifier␈α
␈↓↓alt␈↓␈α
in␈α
the␈α
above␈α
example␈α
is␈α
bound␈α
by␈α
␈↓↓label␈↓␈α
and␈α
is␈α
local␈α
to␈α∞that␈α
expression,
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *27


␈↓ ↓H␈↓and␈α
this␈α
is␈α
the␈α
general␈α
rule.␈α
 The␈α
label␈α
construct␈αis␈α
not␈α
often␈α
used␈α
in␈α
LISP␈α
since␈α
it␈α
is␈α
more␈αusual␈α
to
␈↓ ↓H␈↓give functions global definitions.



␈↓ ↓H␈↓13.  ␈↓αThe function ␈↓↓eval.␈↓α ␈↓


␈↓ ↓H␈↓        ␈↓↓eval␈↓␈αplays␈αboth␈αa␈αtheoretical␈αand␈αa␈αpractical␈αrole␈αin␈αLISP.␈α Historically,␈αthe␈αlist␈αnotation␈αfor
␈↓ ↓H␈↓LISP␈αfunctions␈αand␈α␈↓↓eval␈↓␈α
were␈αfirst␈αdevised␈αin␈α
order␈αto␈αshow␈αhow␈αeasy␈α
it␈αis␈αto␈αdefine␈α
a␈αuniversal
␈↓ ↓H␈↓function␈α
in␈αLISP␈α
-␈αthe␈α
idea␈α
was␈αto␈α
advocate␈αLISP␈α
as␈α
an␈αalternative␈α
to␈αTuring␈α
machines␈αfor␈α
doing
␈↓ ↓H␈↓the␈α∂elementary␈α⊂theory␈α∂of␈α∂computability.␈α⊂ This␈α∂role␈α∂will␈α⊂be␈α∂discussed␈α∂in␈α⊂a␈α∂later␈α∂chapter.␈α⊂  S.␈α∂R.
␈↓ ↓H␈↓Russell␈α∞noted␈α∞that␈α∞␈↓↓eval␈↓␈α∞could␈α∞serve␈α∞as␈α∞an␈α∞interpreter␈α∞for␈α∞LISP␈α∞and␈α∞promptly␈α∞programmed␈α∞it␈α∞in
␈↓ ↓H␈↓machine␈α
language␈α
with␈α
minor␈α
modifications␈α
to␈α
make␈α
it␈α
more␈α
practical.␈α
 An␈α
interpreter␈α
based␈α
on
␈↓ ↓H␈↓␈↓↓eval␈↓␈αhas␈αremained␈αa␈αfeature␈αof␈αmost␈αLISP␈αsystems.␈α Thus␈αwhen␈αyou␈αtalking␈αto␈αLISP␈αthe␈αsystem␈αis
␈↓ ↓H␈↓in␈αa␈αloop␈αthat␈α␈↓↓read␈↓s␈αwhat␈αyou␈αtype,␈α␈↓↓eval␈↓s␈αit␈αand␈α␈↓↓print␈↓s␈αthe␈αresult.␈α  [Of␈αcourse␈αa␈αreal␈αLISP␈αsystem
␈↓ ↓H␈↓does many other things too, such a storage management, error handling, etc.]

␈↓ ↓H␈↓        ␈↓↓eval␈↓␈αfor␈αLISP␈αexpressions␈αis␈αanalogous␈αto␈αthe␈αinterpreter␈α␈↓↓numval␈↓␈αfor␈αarithmetic␈αexpressions
␈↓ ↓H␈↓given␈αin␈α(9.4).␈α The␈αfirst␈αargument␈αto␈α␈↓↓eval␈↓␈αis␈αa␈αLISP␈αexpression␈αin␈αinternal␈αnotation.␈α The␈αsecond
␈↓ ↓H␈↓argument␈α∞is␈α∂an␈α∞association␈α∞list␈α∂that␈α∞tells␈α∞␈↓↓eval␈↓␈α∂what␈α∞value␈α∞each␈α∂variable␈α∞has,␈α∞and␈α∂what␈α∞function
␈↓ ↓H␈↓definition␈αis␈αto␈αbe␈αassociated␈αwith␈αeach␈αfunction␈αname.␈α Thus␈αthe␈αassociation␈αlist␈αis␈αa␈αlist␈αof␈αpairs
␈↓ ↓H␈↓where␈αeach␈α
pair␈αconsists␈α
either␈αof␈α
a␈αvariable␈α
and␈αthe␈α
S-expression␈αcorresponding␈α
to␈αits␈α
value,␈αor␈α
a
␈↓ ↓H␈↓function␈α
name␈α∞and␈α
the␈α∞S-expression␈α
representing␈α∞the␈α
function␈α∞expression␈α
defining␈α∞the␈α
function.
␈↓ ↓H␈↓(Here␈α∂a␈α∂function␈α∂expression␈α∂is␈α∞either␈α∂a␈α∂function␈α∂name,␈α∂or␈α∞a␈α∂lambda␈α∂expression.)␈α∂The␈α∂result␈α∞of
␈↓ ↓H␈↓applying␈α␈↓↓eval␈↓␈αis␈αthe␈αvalue␈αof␈αthe␈α
term␈αrepresented␈αby␈αthe␈αS-expression␈αin␈αan␈α
environment␈αwhere
␈↓ ↓H␈↓the␈α∞free␈α∂variables␈α∞are␈α∞assigned␈α∂the␈α∞values␈α∞given␈α∂by␈α∞the␈α∞association␈α∂list␈α∞and␈α∞where␈α∂the␈α∞function
␈↓ ↓H␈↓names␈α∪occuring␈α∩free␈α∪(i.e.␈α∩not␈α∪bound␈α∩in␈α∪a␈α∩label␈α∪expression)␈α∩denote␈α∪functions␈α∩defined␈α∪by␈α∩the
␈↓ ↓H␈↓associated expressions in the association list.

␈↓ ↓H␈↓        Since␈αany␈αcomputation␈αcan␈αbe␈αdescribed␈αas␈αevaluating␈αan␈αexpression␈αwithout␈αfree␈αvariables
␈↓ ↓H␈↓or␈α∀function␈α∀names,␈α∀the␈α∀second␈α∀argument␈α∀theoretically␈α∀plays␈α∀a␈α∀role␈α∀mainly␈α∀in␈α∃the␈α∀recursive
␈↓ ↓H␈↓definition of ␈↓↓eval.␈↓  Thus we usually start our computations with the second argument ␈↓¬NIL␈↓.

␈↓ ↓H␈↓        To␈αillustrate␈αthis,␈αsuppose␈αwe␈αwant␈αto␈αapply␈αthe␈αfunction␈α␈↓↓alt␈↓␈αto␈αthe␈αlist␈α␈↓¬(A␈αB␈αC␈αD␈αE)␈↓,␈αi.e.␈αwe
␈↓ ↓H␈↓wish to evaluate ␈↓↓alt[␈↓¬(A B C D E)␈↓↓]␈↓.  This can be obtained by computing

␈↓ ↓H␈↓                      ␈↓↓eval[␈↓␈↓¬((LABEL ALT␈↓
␈↓ ↓H␈↓                                  ␈↓¬(LAMBDA (X) (COND ((OR (NULL X) (NULL (CDR X))) X)␈↓
␈↓ ↓H␈↓                                                           ␈↓¬(T (CONS (CAR X) (ALT (CDDR X)))))))␈↓
␈↓ ↓H␈↓                                 ␈↓¬(QUOTE (A B C D E)))␈↓,
␈↓ ↓H␈↓                              ␈↓↓␈↓¬NIL␈↓↓]␈↓,

␈↓ ↓H␈↓which gives the expected result ␈↓¬(A C E)␈↓.

␈↓ ↓H␈↓        This␈α∂manner␈α∞of␈α∂evaluating␈α∞requires␈α∂that␈α∞auxiliary␈α∂functions␈α∞must␈α∂be␈α∞defined␈α∂within␈α∞any
␈↓ ↓H␈↓expression␈αwhere␈αthey␈αare␈αused␈α(by␈αusing␈αthe␈αlabel␈αconstruct)␈αand␈αworse␈αyet,␈αthey␈αmust␈αbe␈αdefined
␈↓ ↓H␈↓separately␈α↔for␈α⊗separate␈α↔occurrences.␈α⊗  This␈α↔can␈α⊗become␈α↔very␈α⊗cumbersome␈α↔and␈α↔also␈α⊗makes
␈↓ ↓H␈↓expressions␈α∞fairly␈α∞difficult␈α
to␈α∞understand.␈α∞ Another␈α
approach␈α∞is␈α∞to␈α
use␈α∞an␈α∞association␈α∞list␈α
which
␈↓ ↓H␈↓associates␈α⊗each␈α∃function␈α⊗name␈α⊗appearing␈α∃in␈α⊗the␈α⊗expression␈α∃with␈α⊗the␈α⊗appropriate␈α∃defining
␈↓ ↓H␈↓expression.   Thus we could evaluate ␈↓↓alt[␈↓¬(A B C D E)␈↓↓]␈↓ by computing
␈↓ ↓H␈↓28␈↓ εβChapter  I␈↓ H



␈↓ ↓H␈↓↓                        eval[␈↓¬(ALT (QUOTE (A B C D E)))␈↓↓,
␈↓ ↓H␈↓↓                              ␈↓¬((ALT LAMBDA (X) (COND ((OR (NULL X) (NULL (CDR X))) X)␈↓↓
␈↓ ↓H␈↓↓                                                           ␈↓¬(T (CONS (CAR X) (ALT (CDDR X)))))))␈↓↓].

␈↓ ↓H␈↓        A simplified version of the usual LISP ␈↓↓eval␈↓ is the following:

␈↓ ↓H␈↓␈↓ α8␈↓↓eval[e, a] ←␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ [␈↓αif␈↓↓ numberp e ␈↓αor␈↓↓ e  = ␈↓¬NIL␈↓↓ ␈↓αor␈↓↓ e = ␈↓¬T␈↓↓ ␈↓αthen␈↓↓ e  ␈↓αelse␈↓↓ ␈↓αd|␈↓↓assoc[e, a]]␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓e ␈↓αthen␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓[␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬QUOTE ␈↓↓␈↓αthen␈↓↓ ␈↓αad|␈↓↓e␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬COND ␈↓↓␈↓αthen␈↓↓ evcond[␈↓αd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬LIST ␈↓↓␈↓αthen␈↓↓ evlist[␈↓αd|␈↓↓e,a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CAR ␈↓↓␈↓αthen␈↓↓ ␈↓αa|␈↓↓eval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓13.1)␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CDR ␈↓↓␈↓αthen␈↓↓ ␈↓αd|␈↓↓eval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬CONS ␈↓↓␈↓αthen␈↓↓ eval[␈↓αad|␈↓↓e, a] . eval[␈↓αadd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬ATOM ␈↓↓␈↓αthen␈↓↓ ␈↓αat|␈↓↓eval[␈↓αad|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓e = ␈↓¬EQ ␈↓↓␈↓αthen␈↓↓  eval[␈↓αad|␈↓↓e, a] ␈↓αeq␈↓↓ eval[␈↓αadd|␈↓↓e, a]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ eval[␈↓αd|␈↓↓assoc[␈↓αa|␈↓↓e, a] . ␈↓αd|␈↓↓e, a]]␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓e = ␈↓¬LAMBDA ␈↓↓␈↓αthen␈↓↓ eval[␈↓αadda|␈↓↓e, prup[␈↓αada|␈↓↓e, evlist[␈↓αd|␈↓↓e,a]] * a]␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓e = ␈↓¬LABEL ␈↓↓␈↓αthen␈↓↓ eval[␈↓αadda|␈↓↓e . ␈↓αd|␈↓↓e, [␈↓αada|␈↓↓e . ␈↓αadda|␈↓↓e] . a]␈↓,


␈↓ ↓H␈↓where the auxiliary functions ␈↓↓evcond␈↓ and ␈↓↓evlist␈↓ are defined by

␈↓ ↓H␈↓13.2) ␈↓ α⎇␈↓↓evcond[u, a] ← ␈↓αif␈↓↓ eval[␈↓αaa|␈↓↓u, a] ␈↓αthen␈↓↓ eval[␈↓αada|␈↓↓u, a]  ␈↓αelse␈↓↓ evcond[␈↓αd|␈↓↓u, a]␈↓, 

␈↓ ↓H␈↓13.3) ␈↓ β<␈↓↓evlist[u, a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ eval[␈↓αa|␈↓↓u,a] . evlist[␈↓αd|␈↓↓u, a]␈↓, 

␈↓ ↓H␈↓and␈αthe␈αauxiliary␈αfunction␈α␈↓↓prup,␈↓␈αused␈αfor␈αpairing␈αup␈αthe␈αelements␈αof␈αtwo␈αlists␈αof␈αequal␈αlength,␈αis
␈↓ ↓H␈↓defined by

␈↓ ↓H␈↓13.4) ␈↓ βA␈↓↓prup[u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [␈↓αa|␈↓↓u . ␈↓αa|␈↓↓v] . prup[␈↓αd|␈↓↓u,␈↓αd|␈↓↓v]␈↓. 

␈↓ ↓H␈↓Recall that ␈↓↓assoc␈↓ is defined by

␈↓ ↓H␈↓13.5) ␈↓ α{␈↓↓assoc[x,a] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓a ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αaa|␈↓↓a ␈↓αeq␈↓↓ x ␈↓αthen␈↓↓ ␈↓αa|␈↓↓a ␈↓αelse␈↓↓ assoc[x,␈↓αd|␈↓↓a].␈↓ 

␈↓ ↓H␈↓        This␈α⊂simple␈α⊃␈↓↓eval␈↓␈α⊂expects␈α⊃that␈α⊂an␈α⊃expression␈α⊂is␈α⊃either␈α⊂a␈α⊃constant␈α⊂(␈↓↓number,␈↓␈α⊃␈↓¬T␈↓,␈α⊂␈↓¬NIL␈↓,␈α⊃or␈α⊂a
␈↓ ↓H␈↓␈↓¬QUOTE␈↓d␈αS-expression),␈αa␈αvariable␈α
whose␈αvalue␈αcan␈αbe␈αfound␈α
on␈αthe␈αassociation␈αlist,␈α
a␈αconditional
␈↓ ↓H␈↓expression,␈α
a␈αlist␈α
making␈αexpression,␈α
or␈αan␈α
application␈αof␈α
a␈αfunction,␈α
lambda␈αexpression␈α
or␈αlabel
␈↓ ↓H␈↓expression␈α∩to␈α∩a␈α∩list␈α∩of␈α∩arguments.␈α∩ Thus␈α∪␈↓↓eval␈↓␈α∩checks␈α∩to␈α∩see␈α∩which␈α∩of␈α∩the␈α∩above␈α∪classes␈α∩the
␈↓ ↓H␈↓expression␈α
to␈α
be␈α
evaluated␈α
falls␈α
into␈α
and␈α
proceeds␈α
accordingly.␈α
 If␈α
the␈α
expression,␈α
␈↓↓e,␈↓␈α
is␈α
atomic␈α
then
␈↓ ↓H␈↓it␈α∩is␈α∩either␈α∩a␈α∩non␈α⊃␈↓¬QUOTE␈↓d␈α∩constant␈α∩or␈α∩a␈α∩variable.␈α∩ If␈α⊃the␈α∩former␈α∩then␈α∩␈↓↓eval␈↓␈α∩just␈α∩returns␈α⊃the
␈↓ ↓H␈↓expression, if the latter it looks up the value on the association list, ␈↓↓a.␈↓

␈↓ ↓H␈↓        If␈α␈↓↓e␈↓␈αis␈αnon-atomic␈αbut␈α
␈↓αa|␈↓␈↓↓e␈↓␈αis␈αatomic␈αthen␈α␈↓↓e␈↓␈αis␈α
either␈αa␈α␈↓¬QUOTE␈↓d␈αconstant,␈αa␈αconditional,␈α
a␈αlist
␈↓ ↓H␈↓maker,␈αor␈αan␈αapplication␈αof␈αa␈αfunction␈αto␈αa␈αlist␈αof␈αarguments.␈α In␈αthe␈αconstant␈αcase␈αthe␈αexpression
␈↓ ↓H␈↓being␈α∂quoted␈α∞(␈↓αad|␈↓␈↓↓e)␈↓␈α∂is␈α∞returned.␈α∂ If␈α∞␈↓↓e␈↓␈α∂is␈α∂a␈α∞conditional␈α∂then␈α∞the␈α∂list␈α∞of␈α∂pairs␈α∞is␈α∂processed␈α∂by␈α∞the
␈↓ ↓H␈↓auxiliary␈α
evaluator␈α
␈↓↓evcond,␈↓␈α
which␈α
␈↓↓eval␈↓s␈α
the␈α
"if"␈α
parts␈α
(␈↓↓␈↓αaa|␈↓↓u␈↓)␈α
in␈α
order␈α
until␈α
a␈α
true␈α
one␈α
is␈α
found␈α
then
␈↓ ↓H␈↓␈↓ εβChapter  I␈↓ *29


␈↓ ↓H␈↓returns␈α⊂the␈α∂result␈α⊂of␈α∂␈↓↓eval␈↓ing␈α⊂the␈α∂corresponding␈α⊂"then"␈α∂part␈α⊂(␈↓↓␈↓αada|␈↓↓u␈↓).␈α∂ In␈α⊂the␈α∂list␈α⊂case␈α∂the␈α⊂list␈α∂of
␈↓ ↓H␈↓expressions␈α
to␈α
be␈αevaluated␈α
is␈α
given␈αto␈α
␈↓↓evlist␈↓␈α
which␈α
returns␈αa␈α
list␈α
of␈αthe␈α
values.␈α
 In␈α
the␈αfunction
␈↓ ↓H␈↓application␈αcase␈αthere␈αare␈αtwo␈αpossibilities.␈α If␈αthe␈αfunction␈αto␈αbe␈αapplied␈αis␈αone␈αof␈αthe␈αelementary
␈↓ ↓H␈↓functions,␈α∀the␈α∀indicated␈α∀operation␈α∀is␈α∀performed␈α∀on␈α∀the␈α∀result␈α∀of␈α∀evaluating␈α∀the␈α∀arguments.
␈↓ ↓H␈↓Otherwise␈α
the␈α
function␈α
must␈α
be␈α
defined␈α
in␈α
␈↓↓a,␈↓␈α
so␈α
␈↓↓eval␈↓␈α
looks␈α
up␈α
the␈α
definition,␈α
replaces␈α
the␈α
function
␈↓ ↓H␈↓name by the function definition in the expression and restarts the evaluation.

␈↓ ↓H␈↓        If␈α
neither␈α␈↓↓e␈↓␈α
nor␈α␈↓αa|␈↓␈↓↓e␈↓␈α
are␈αatomic␈α
then␈αit␈α
must␈α
be␈αa␈α
lambda␈αor␈α
label␈αapplication.␈α
 In␈αthe␈α
lambda
␈↓ ↓H␈↓case␈αthe␈αargument␈α
list␈αis␈αgiven␈αto␈α
␈↓↓evlist␈↓␈αto␈αbe␈αevaluated,␈α
the␈αvalues␈αare␈αthen␈α
paired␈αwith␈αthe␈αlist␈α
of
␈↓ ↓H␈↓variables␈αto␈αbe␈αbound␈αby␈αthe␈αlambda␈α(␈↓αada|␈↓␈↓↓e)␈↓␈αusing␈α␈↓↓prup␈↓␈αand␈αput␈αon␈αthe␈αfront␈αof␈α␈↓↓a.␈↓␈αThe␈αbody␈αof
␈↓ ↓H␈↓the␈α∂lambda␈α∂(␈↓αadda|␈↓␈↓↓e)␈↓␈α∂is␈α∂then␈α⊂evaluated␈α∂using␈α∂this␈α∂new␈α∂association␈α⊂list.␈α∂ In␈α∂the␈α∂label␈α∂case␈α⊂a␈α∂new
␈↓ ↓H␈↓association␈α∩list␈α∩is␈α∩formed␈α∩by␈α∩pairing␈α⊃the␈α∩function␈α∩name␈α∩(␈↓αada|␈↓␈↓↓e)␈↓␈α∩with␈α∩the␈α∩defining␈α⊃expression
␈↓ ↓H␈↓(␈↓αadda|␈↓␈↓↓e)␈↓␈αand␈α
adding␈αthe␈α
result␈αto␈α
the␈αfront␈α
of␈α␈↓↓a.␈↓␈α
 Then␈αthe␈α
label␈αexpression␈α
is␈αreplaced␈α
in␈α␈↓↓e␈↓␈αby␈α
the
␈↓ ↓H␈↓defining expression and this is evaluated using the new association list.

␈↓ ↓H␈↓        If␈α∞␈↓↓e␈↓␈α
is␈α∞not␈α∞an␈α
expression␈α∞of␈α
the␈α∞sort␈α∞expected␈α
by␈α∞␈↓↓eval,␈↓␈α
then␈α∞the␈α∞result␈α
is␈α∞not␈α∞defined.␈α
  It
␈↓ ↓H␈↓would␈α
not␈α
be␈α
difficult␈α
to␈α
add␈α
additional␈α∞clauses␈α
to␈α
␈↓↓eval␈↓␈α
so␈α
that␈α
it␈α
would␈α
return␈α∞reasonable␈α
error
␈↓ ↓H␈↓messages␈αrather␈α
than␈αjust␈α
being␈αundefined␈α(or␈α
dying␈αin␈α
some␈αstrange␈αway␈α
as␈αwould␈α
be␈αlikely␈αin␈α
an
␈↓ ↓H␈↓actual␈α⊃computer).␈α⊃ It␈α⊃would␈α⊂be␈α⊃necessary␈α⊃to␈α⊃be␈α⊂make␈α⊃the␈α⊃error␈α⊃messages␈α⊃distinguishable␈α⊂from
␈↓ ↓H␈↓legitimate␈αvalues␈αof␈αthe␈αexpression␈αso␈αthat␈αerrors␈αat␈αinner␈αlevels␈αof␈αevaluation␈αcould␈αbe␈αpassed␈αup
␈↓ ↓H␈↓the␈αchain.␈α This␈αcould␈αbe␈αdone␈αby␈αreturning␈αlegitimate␈αvalues␈αas␈αpairs␈αwhose␈αfirst␈αelement␈αis␈αone
␈↓ ↓H␈↓atom␈α∂and␈α∂error␈α∂messages␈α⊂as␈α∂pairs␈α∂prefixed␈α∂by␈α∂another.␈α⊂ Terms␈α∂containing␈α∂␈↓↓eval␈↓␈α∂would␈α⊂have␈α∂to
␈↓ ↓H␈↓distinguish the cases.

␈↓ ↓H␈↓        Notice␈α⊗that␈α⊗␈↓¬COND␈α⊗␈↓and␈α⊗␈↓¬LIST␈α⊗␈↓considered␈α⊗as␈α⊗pseudo-functions␈α⊗behave␈α⊗differently␈α⊗than
␈↓ ↓H␈↓ordinary␈α⊃functions␈α⊃in␈α∩that␈α⊃both␈α⊃can␈α∩take␈α⊃an␈α⊃arbitrary␈α∩number␈α⊃of␈α⊃arguments␈α∩while␈α⊃functions
␈↓ ↓H␈↓defined␈α
by␈α
a␈α
lambda␈α
expression␈α
have␈α
a␈α
fixed␈α
number␈α
of␈α
arguments␈α
determined␈α
by␈α
the␈αvariable
␈↓ ↓H␈↓list␈αoccuring␈αin␈αthe␈αlambda␈αexpression.␈α Also,␈αthe␈αusual␈αmanner␈αof␈αevaluation␈αan␈αapplication␈αterm
␈↓ ↓H␈↓is␈αLISP␈αis␈αto␈αevaluate␈αall␈αof␈αthe␈αarguments␈αthen␈αapply␈αthe␈αfunction.␈α This␈αwill␈αnot␈αwork␈αfor␈α␈↓¬COND
␈↓ ↓H␈↓¬␈↓as␈αthe␈αmain␈αreason␈αfor␈αa␈αconditional␈αis␈αto␈αbe␈αable␈αto␈αselect␈αa␈αterm␈αto␈αevaluate␈αdepending␈αon␈αsome
␈↓ ↓H␈↓set of conditions and not to evaluate other terms under those conditions.

␈↓ ↓H␈↓        The␈α∞above␈α∞version␈α∞of␈α
␈↓↓eval␈↓␈α∞does␈α∞not␈α∞handle␈α∞the␈α
propositional␈α∞constructs␈α∞␈↓αand␈↓,␈α∞␈↓αor␈↓,␈α∞and␈α
␈↓αnot␈↓.
␈↓ ↓H␈↓The␈α
effect␈α∞of␈α
these␈α∞constructs␈α
can␈α
be␈α∞obtained␈α
by␈α∞appropriate␈α
use␈α
of␈α∞the␈α
condtional,␈α∞but␈α
simply
␈↓ ↓H␈↓defining␈α⊃functions␈α⊂for␈α⊃␈↓αand␈↓␈α⊂and␈α⊃␈↓αor␈↓␈α⊂will␈α⊃not␈α⊂work␈α⊃as␈α⊂the␈α⊃evaluation␈α⊂of␈α⊃a␈α⊃function␈α⊂application
␈↓ ↓H␈↓requires␈α
that␈α
all␈α
of␈α
the␈αarguments␈α
of␈α
of␈α
the␈α
function␈α
be␈αevaluated␈α
before␈α
it␈α
is␈α
applied␈α
while␈αthe
␈↓ ↓H␈↓specification␈α⊂of␈α⊂␈↓αand␈↓␈α⊂and␈α⊂␈↓αor␈↓␈α⊂require␈α⊂that␈α⊂only␈α⊂as␈α⊂many␈α⊂of␈α⊂the␈α⊂arguments␈α⊂be␈α⊂evaluated␈α⊃as␈α⊂are
␈↓ ↓H␈↓required␈α∂to␈α⊂determine␈α∂the␈α∂answer.␈α⊂ Another␈α∂problem␈α⊂is␈α∂that␈α∂in␈α⊂most␈α∂implementations␈α⊂they␈α∂are
␈↓ ↓H␈↓allowed␈α∞to␈α
take␈α∞an␈α
arbitrary␈α∞number␈α
of␈α∞arguments.␈α∞  Thus␈α
we␈α∞need␈α
to␈α∞build␈α
them␈α∞into␈α∞␈↓↓eval␈↓␈α
for
␈↓ ↓H␈↓things␈α
to␈α
work␈α∞properly.␈α
 We␈α
will␈α
see␈α∞later␈α
that␈α
LISP␈α
systems␈α∞provide␈α
alternate␈α
solutions␈α∞to␈α
this
␈↓ ↓H␈↓problem␈α
by␈α
providing␈α
a␈α
variety␈α
of␈α
modes␈α
of␈α
functions␈α
application.␈α
 (These␈α
are␈α
known␈α∞as␈α
␈↓¬EXPR,
␈↓ ↓H␈↓¬␈↓␈↓¬FEXPR␈α⊃␈↓and␈α⊃␈↓¬LEXPR␈↓s.)␈α⊃   Arithmetic␈α∩is␈α⊃also␈α⊃missing␈α⊃from␈α∩our␈α⊃␈↓↓eval.␈↓␈α⊃ Adding␈α⊃these␈α∩constructs␈α⊃is
␈↓ ↓H␈↓essentially␈α∩like␈α⊃combining␈α∩␈↓↓eval␈↓␈α⊃with␈α∩the␈α∩earlier␈α⊃evaluator␈α∩␈↓↓numval␈↓␈α⊃and␈α∩adding␈α∩any␈α⊃additional
␈↓ ↓H␈↓primitive operations that are desired.

␈↓ ↓H␈↓        We␈α
note␈α∞that␈α
␈↓↓eval␈↓␈α
can␈α∞evaluate␈α
itself␈α∞if␈α
it␈α
is␈α∞given␈α
an␈α
association␈α∞list␈α
containing␈α∞the␈α
pairs
␈↓ ↓H␈↓␈↓¬(EVAL . λeval),␈α(␈↓␈↓¬(EVCOND . λevcond),␈α(␈↓␈↓¬(EVLIST . λevlist),␈α(␈↓␈α(␈↓¬(ASSOC . λassoc),
␈↓ ↓H␈↓¬␈↓␈↓¬(PRUP . λprup),␈α␈↓and␈α
␈↓¬(APPEND . λappend)␈α␈↓␈α
where␈α␈↓¬λ<function name>␈α
␈↓stands␈αfor␈α
the␈αinternal
␈↓ ↓H␈↓form of the expression defining the named function.  Thus

␈↓ ↓H␈↓␈↓ ∧'␈↓↓eval[␈↓¬(EVAL '(CAR '(A.B)) NIL)␈↓↓,alist] = ␈↓¬A␈↓↓␈↓ 
␈↓ ↓H␈↓30␈↓ εβChapter  I␈↓ H


␈↓ ↓H␈↓where␈α
␈↓↓alist␈↓␈αis␈α
some␈αassociation␈α
list␈αcontaining␈α
the␈α
above␈αmentioned␈α
pairs␈α(and␈α
no␈αother␈α
definitions
␈↓ ↓H␈↓of those functions).

␈↓ ↓H␈↓        When␈αyou␈αtalk␈αto␈αLISP␈αyou␈αdo␈αnot␈αexplicitly␈αtell␈αthe␈αinterpreter␈αwhat␈αassociation␈αlist␈αto␈αuse
␈↓ ↓H␈↓generally.␈α
 This␈α
is␈αbecause␈α
the␈α
LISP␈α
associates␈αwith␈α
each␈α
atom␈α
a␈αlist␈α
of␈α
properties,␈α
 among␈αthese
␈↓ ↓H␈↓are␈α∂the␈α∂value,␈α∂and/or␈α∂the␈α∂function␈α∞definition␈α∂associated␈α∂with␈α∂that␈α∂atom.␈α∂ The␈α∂LISP␈α∞interpreter
␈↓ ↓H␈↓typically␈α∂looks␈α∂up␈α∂variable␈α∂values␈α∂and␈α∞function␈α∂definitions␈α∂on␈α∂the␈α∂corresponding␈α∂property␈α∞lists.
␈↓ ↓H␈↓Thus,␈α⊃instead␈α⊃of␈α⊃making␈α⊃up␈α⊃an␈α⊂association␈α⊃list␈α⊃with␈α⊃the␈α⊃appropriate␈α⊃variables␈α⊃and␈α⊂functions
␈↓ ↓H␈↓defined,␈αthe␈αproperty␈αlists␈αmust␈αfirst␈αbe␈αprimed␈αby␈αdoing␈α␈↓¬SETQ␈↓s␈αfor␈αgiving␈αvariables␈αinitial␈αvalues
␈↓ ↓H␈↓and␈α⊂␈↓¬DEFUN␈↓s␈α⊂(or␈α⊂␈↓¬DEFPROP␈↓s)␈α⊂for␈α∂any␈α⊂functions␈α⊂that␈α⊂need␈α⊂to␈α∂be␈α⊂defined.␈α⊂ These␈α⊂features␈α⊂will␈α∂be
␈↓ ↓H␈↓discussed in more detail in Chapter V.

␈↓ ↓H␈↓α␈↓ εεExercises.



␈↓ ↓H␈↓1. What is the value of

␈↓ ↓H␈↓    ␈↓↓eval[␈↓¬(LEFT (QUOTE (A . B)))␈↓↓,␈↓
␈↓ ↓H␈↓    ␈↓↓      ␈↓¬((LEFT LAMBDA (X) (COND ((ATOM X) X) (T (LEFT (CAR X))))))␈↓↓]␈↓?


␈↓ ↓H␈↓2. Translate the definition of ␈↓↓eval␈↓ given above into internal notation.

␈↓ ↓H␈↓3.␈αGo␈αto␈αyour␈αnearest␈αLISP␈αsystem,␈αtype␈αin␈αyour␈αanswer␈αto␈αthe␈αsecond␈αexercise␈αand␈αuse␈αit␈αto␈αcheck
␈↓ ↓H␈↓your answer to the first exercise.
␈↓ ↓H␈↓␈↓ εH␈↓ *31


␈↓ ↓H␈↓α␈↓ εChapter II

␈↓ ↓H␈↓α␈↓ βZWRITING RECURSIVE FUNCTION DEFINITIONS


␈↓ ↓H␈↓        In␈αthe␈αChapter␈αI␈αwe␈αdiscussed␈αthe␈αbasic␈αconstructs␈αof␈αLISP␈αand␈αexplained␈αhow␈αto␈αevaluate
␈↓ ↓H␈↓terms␈αbuilt␈αup␈αusing␈αthese␈αconstructs.␈α The␈αnotion␈αof␈αrecursively␈αdefined␈αfunction␈αwas␈αintroduced
␈↓ ↓H␈↓and␈αthe␈αrules␈αfor␈αcomputing␈αthe␈αvalue␈α
of␈αa␈αrecursively␈αdefined␈αfunction␈αwere␈αgiven.␈α
 In␈αaddition
␈↓ ↓H␈↓we␈α∂showed␈α∞how␈α∂LISP␈α∂programs␈α∞are␈α∂represented␈α∞as␈α∂S-expressions␈α∂and␈α∞how␈α∂these␈α∂programs␈α∞are
␈↓ ↓H␈↓interpretered␈α
by␈α
the␈α
function␈α
␈↓↓eval.␈↓␈α  By␈α
now␈α
you␈α
should␈α
be␈αable␈α
to␈α
read␈α
and␈α
understand␈αsimple
␈↓ ↓H␈↓LISP␈α∂programs.␈α∂ The␈α⊂next␈α∂step␈α∂is␈α⊂learning␈α∂to␈α∂write␈α∂LISP␈α⊂programs.␈α∂ In␈α∂principle␈α⊂you␈α∂already
␈↓ ↓H␈↓know␈α
all␈α
that␈α
is␈α
necessary,␈αhowever␈α
there␈α
are␈α
some␈α
basic␈αideas␈α
and␈α
 techniques␈α
which␈α
are␈αuseful␈α
in
␈↓ ↓H␈↓solving␈αLISP␈αprogramming␈αproblems.␈α
 The␈αpurpose␈αof␈αthis␈αchapter␈α
is␈αto␈αhelp␈αyou␈αlearn␈α
to␈αthink
␈↓ ↓H␈↓recursively␈α⊃and␈α⊃to␈α⊃familiarize␈α⊃you␈α⊃with␈α⊃some␈α⊃of␈α⊃the␈α⊃basic␈α⊃techniques␈α⊃and␈α⊃standard␈α⊃forms␈α⊃of
␈↓ ↓H␈↓recursive␈α∪programs.␈α∪ The␈α∪final␈α∪section␈α∪contains␈α∪a␈α∪collection␈α∪of␈α∪problems␈α∪of␈α∪varying␈α∩degrees
␈↓ ↓H␈↓difficulty for you practice on.



␈↓ ↓H␈↓1.  ␈↓αStatic and dynamic ways of programming.␈↓


␈↓ ↓H␈↓        In␈α∃order␈α∀to␈α∃write␈α∀recursive␈α∃function␈α∀definitions,␈α∃one␈α∀must␈α∃think␈α∃about␈α∀programming
␈↓ ↓H␈↓differently␈α
than␈α
is␈α
customary␈α
when␈α
writing␈α
programs␈α
in␈α
languages␈α
like␈α
FORTRAN␈α
or␈αALGOL␈α
or
␈↓ ↓H␈↓in␈α⊃machine␈α∩language.␈α⊃ In␈α∩these␈α⊃languages,␈α⊃one␈α∩has␈α⊃in␈α∩mind␈α⊃the␈α⊃state␈α∩of␈α⊃the␈α∩computation␈α⊃as
␈↓ ↓H␈↓represented␈α∞by␈α∂the␈α∞values␈α∂of␈α∞certain␈α∞variables␈α∂or␈α∞locations␈α∂in␈α∞the␈α∞memory␈α∂of␈α∞the␈α∂machine,␈α∞and
␈↓ ↓H␈↓then␈α⊃one␈α⊃writes␈α⊃statements␈α⊃or␈α⊃machine␈α⊃instructions␈α∩in␈α⊃order␈α⊃to␈α⊃make␈α⊃the␈α⊃state␈α⊃change␈α∩in␈α⊃an
␈↓ ↓H␈↓appropriate␈α∂way.␈α⊂ When␈α∂writing␈α∂recursive␈α⊂function␈α∂definitions␈α∂one␈α⊂takes␈α∂a␈α⊂different␈α∂approach.
␈↓ ↓H␈↓Namely,␈α
one␈α∞thinks␈α
about␈α∞the␈α
value␈α∞of␈α
the␈α
function,␈α∞asks␈α
for␈α∞what␈α
values␈α∞of␈α
the␈α∞arguments␈α
the
␈↓ ↓H␈↓value␈α
of␈αthe␈α
function␈αis␈α
immediate,␈α
and,␈αgiven␈α
arbitrary␈αvalues␈α
of␈α
the␈αarguments,␈α
for␈αwhat␈α
simpler
␈↓ ↓H␈↓arguments␈α
must␈α
the␈α
function␈α
be␈αknown␈α
in␈α
order␈α
to␈α
give␈α
the␈αvalue␈α
of␈α
the␈α
function␈α
for␈α
the␈αgiven
␈↓ ↓H␈↓arguments.

␈↓ ↓H␈↓        Let␈αus␈αconsider␈αa␈αnumerical␈αexample;␈αnamely,␈αsuppose␈αwe␈αwant␈αto␈αcompute␈αthe␈αfunction␈α␈↓↓n!.␈↓
␈↓ ↓H␈↓For␈αwhat␈αargument␈αis␈αthe␈αvalue␈αof␈αthe␈αfunction␈αimmediate.␈α Clearly,␈αfor␈α␈↓↓n␈↓␈α=␈α0␈αor␈α␈↓↓n␈↓␈α=␈α1,␈αthe␈αvalue
␈↓ ↓H␈↓is␈α
immediately␈α
seen␈α
to␈α
be␈α
1.␈α Moreover,␈α
we␈α
can␈α
get␈α
the␈α
value␈αfor␈α
an␈α
arbitrary␈α
␈↓↓n␈↓␈α
if␈α
we␈α
know␈αthe
␈↓ ↓H␈↓value␈α⊂for␈α⊂␈↓↓n-1␈↓.␈α⊂ Also,␈α∂we␈α⊂see␈α⊂that␈α⊂knowing␈α⊂the␈α∂value␈α⊂for␈α⊂␈↓↓n␈↓␈α⊂=␈α⊂1␈α∂is␈α⊂redundant,␈α⊂since␈α⊂it␈α⊂can␈α∂be
␈↓ ↓H␈↓obtained␈αfrom␈αthe␈α␈↓↓n␈↓␈α=␈α0␈αcase␈αby␈αthe␈αsame␈αrule␈αas␈αgets␈αit␈αfor␈αa␈αgeneral␈α␈↓↓n␈↓␈αfrom␈αthe␈αvalue␈αfor␈α␈↓↓n-1␈↓.
␈↓ ↓H␈↓All this talk leads to the simple recursive definition:

␈↓ ↓H␈↓1.1) ␈↓ ∧q␈↓↓n! ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ n␈↓π*␈↓↓[n-␈↓¬1␈↓↓]!␈↓. 

␈↓ ↓H␈↓        We␈αmay␈αregard␈αthis␈αas␈αa␈αstatic␈αway␈αof␈αlooking␈αat␈αprogramming.␈α We␈αask␈αwhat␈αsimpler␈α
cases
␈↓ ↓H␈↓the␈αgeneral␈αcase␈αof␈αour␈αfunction␈αdepends␈αon␈αrather␈αthan␈αhow␈αwe␈αbuild␈αup␈αthe␈αdesired␈αstate␈αof␈αthe
␈↓ ↓H␈↓computation.

␈↓ ↓H␈↓        An␈α
example␈α
of␈αthe␈α
dynamic␈α
approach␈αto␈α
programming␈α
is␈αthe␈α
following␈α
obvious␈αALGOL␈α
60
␈↓ ↓H␈↓program for computing ␈↓↓n!:␈↓
␈↓ ↓H␈↓32␈↓ ¬}Chapter  II␈↓ H



␈↓ ↓H␈↓↓␈↓ ∧H␈↓αinteger procedure␈↓↓ factorial(n); ␈↓αinteger␈↓↓ s,i;
␈↓ ↓H␈↓↓␈↓ βx␈↓αbegin␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧Hs := ␈↓¬1␈↓↓;
␈↓ ↓H␈↓↓␈↓ ∧Hi := n;
␈↓ ↓H␈↓↓␈↓ βxloop:␈↓ ∧H␈↓αif␈↓↓ i = ␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ done;
␈↓ ↓H␈↓↓1.2)␈↓ ∧Hs := i*s;
␈↓ ↓H␈↓↓␈↓ ∧Hi := i-␈↓¬1␈↓↓;
␈↓ ↓H␈↓↓␈↓ ∧H␈↓αgo to␈↓↓ loop;
␈↓ ↓H␈↓↓␈↓ βxdone:␈↓ ∧Hfactorial := s;
␈↓ ↓H␈↓↓␈↓ βx␈↓αend␈↓↓;


␈↓ ↓H␈↓        One␈αoften␈αis␈αled␈αto␈αbelieve␈αthat␈αstatic␈α=␈αbad␈αand␈α dynamic␈α=␈αgood,␈αbut␈αin␈αthis␈α(particularly
␈↓ ↓H␈↓favorable)␈α∞case␈α∞the␈α∞LISP␈α∞program␈α∞is␈α∞shorter␈α∞and␈α∞clearer.␈α∞ In␈α∞general␈α∞this␈α∞style␈α∞of␈α∂thinking␈α∞and
␈↓ ↓H␈↓programming␈α⊃can␈α⊃produce␈α⊃clean␈α∩simple␈α⊃programs.␈α⊃ It␈α⊃also␈α∩provides␈α⊃a␈α⊃built␈α⊃in␈α∩mechanism␈α⊃of
␈↓ ↓H␈↓problem␈α∞solving␈α∞which␈α∞is␈α∞rather␈α∞like␈α∞what␈α
is␈α∞usually␈α∞called␈α∞␈↓↓subgoaling.␈↓␈α∞ We␈α∞shall␈α∞also␈α∞see␈α
later
␈↓ ↓H␈↓how this style leads to rather natural methods of proving statements about programs.

␈↓ ↓H␈↓         Actually,␈α⊃when␈α⊃we␈α⊂discuss␈α⊃the␈α⊃mechanism␈α⊃of␈α⊂recursion,␈α⊃it␈α⊃will␈α⊂turn␈α⊃out␈α⊃that␈α⊃the␈α⊂LISP
␈↓ ↓H␈↓program␈α⊃is␈α⊃inefficient␈α⊃because␈α⊃it␈α⊃uses␈α⊂the␈α⊃pushdown␈α⊃mechanism␈α⊃unnecessarily␈α⊃and␈α⊃should␈α⊂be
␈↓ ↓H␈↓replaced␈α⊃by␈α∩the␈α⊃following␈α∩somewhat␈α⊃longer␈α⊃program␈α∩that␈α⊃corresponds␈α∩to␈α⊃the␈α∩above␈α⊃ALGOL
␈↓ ↓H␈↓program rather precisely:

␈↓ ↓H␈↓␈↓ ∧<␈↓↓        n! ← fact[n,␈↓¬1␈↓↓],                          ␈↓
␈↓ ↓H␈↓1.3)
␈↓ ↓H␈↓␈↓ ∧+␈↓↓fact[i, s] ← ␈↓αif␈↓↓ i=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ s ␈↓αelse␈↓↓ fact[i-␈↓¬1␈↓↓, i␈↓π*␈↓↓s]␈↓.

␈↓ ↓H␈↓        Perhaps␈α∞the␈α∞distinction␈α∞between␈α∞the␈α∞two␈α∞styles␈α∞is␈α∞equivalent␈α∞to␈α∞what␈α∞some␈α∞people␈α∞call␈α
the
␈↓ ↓H␈↓distinction␈α∩between␈α∩␈↓↓top-down␈↓␈α⊃and␈α∩␈↓↓bottom-up␈↓␈α∩programming␈α⊃with␈α∩␈↓↓static␈↓␈α∩corresponding␈α∩to␈α⊃␈↓↓top-
␈↓ ↓H␈↓↓down.␈↓  LISP offers both, but the static style is better developed in LISP, and we will emphasize it.



␈↓ ↓H␈↓2.  ␈↓αRecursive definition of functions on natural numbers.␈↓


␈↓ ↓H␈↓        In␈αthe␈αnext␈αseveral␈αsections␈αwe␈αexamine␈αvarious␈αforms␈αof␈αrecursive␈αfunction␈αdefinition␈αand
␈↓ ↓H␈↓programs␈α∀having␈α∀such␈α∀forms.␈α∀ We␈α∃begin␈α∀by␈α∀considering␈α∀recursive␈α∀definitions␈α∃of␈α∀numerical
␈↓ ↓H␈↓functions.␈α
 We␈αhave␈α
already␈αseen␈α
one␈αexample,␈α
namely␈αthe␈α
factorial␈αfunction.␈α
 The␈αbasic␈α
idea␈αis␈α
to
␈↓ ↓H␈↓give␈αa␈α
rule␈αfor␈αcomputing␈α
the␈αvalue␈αof␈α
the␈αfunction␈αfor␈α
a␈αparticular␈αvalue␈α
of␈αthe␈αargument␈α
(input)
␈↓ ↓H␈↓in␈αterms␈αof␈αsome␈αcollection␈αof␈αgiven␈α(defined␈αor␈αbuilt␈αin)␈αfunctions␈αand␈αvalues␈αof␈αthe␈αfunction␈αfor
␈↓ ↓H␈↓smaller␈α
arguments.␈α
  Notice␈α
that␈α
we␈α
will␈α
have␈α
to␈α
specify␈α
the␈α
value␈α
for␈α
␈↓¬0␈α
␈↓directly␈α
as␈α
there␈α∞are␈α
no
␈↓ ↓H␈↓smaller␈αnumbers.␈α The␈αmethod␈αof␈αfunction␈αdefinition␈αis␈αparallel␈αto␈αthe␈αdescription␈αof␈α
the␈αdomain
␈↓ ↓H␈↓of␈αnatural␈αnumbers␈αin␈αterms␈αof␈αthe␈αnumber␈α␈↓¬0␈α␈↓and␈αthe␈αoperation␈αof␈αsuccessor,␈αnamely␈αa␈αnumber␈αis
␈↓ ↓H␈↓either ␈↓¬0 ␈↓or obtained by applying successor to a previously constructed number.

␈↓ ↓H␈↓        Recursive␈α∞functions␈α∞of␈α∂natural␈α∞numbers␈α∞have␈α∂the␈α∞the␈α∞subject␈α∂of␈α∞much␈α∞study␈α∂by␈α∞logicians
␈↓ ↓H␈↓and␈α∞mathematicians.␈α∞ One␈α
fairly␈α∞nice␈α∞result␈α∞is␈α
that␈α∞any␈α∞such␈α
function␈α∞can␈α∞be␈α∞computed␈α
starting
␈↓ ↓H␈↓with␈α∂the␈α∂constant␈α⊂␈↓¬0,␈α∂␈↓the␈α∂basic␈α⊂functions␈α∂␈↓↓add1␈↓␈α∂(more␈α∂commonly␈α⊂known␈α∂as␈α∂␈↓↓successor)␈↓␈α⊂␈↓↓sub1␈↓␈α∂(also
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *33


␈↓ ↓H␈↓known␈αas␈α␈↓↓predecessor)␈↓␈α
and␈αthe␈αtools␈αfor␈α
recursive␈αdefinition␈αdescribed␈α
in␈αChapter␈αI.␈α (Actually␈α
one
␈↓ ↓H␈↓can␈α⊂get␈α∂by␈α⊂with␈α∂less,␈α⊂but␈α∂that␈α⊂is␈α⊂not␈α∂the␈α⊂point␈α∂of␈α⊂this␈α∂discussion.)␈α⊂ For␈α∂example␈α⊂the␈α⊂sum␈α∂and
␈↓ ↓H␈↓difference of two numbers are given by

␈↓ ↓H␈↓2.1)␈↓ ∧∂␈↓↓plus[n,m] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ m ␈↓αelse␈↓↓ plus[n-␈↓¬1␈↓↓,m]+␈↓¬1␈↓↓␈↓ 

␈↓ ↓H␈↓2.2)␈↓ α⎇␈↓↓differ[n,m] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ m=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ n ␈↓αelse␈↓↓ differ[n-␈↓¬1␈↓↓,m-␈↓¬1␈↓↓]␈↓ 

␈↓ ↓H␈↓while␈αthe␈αpredicate␈α␈↓↓greaterp␈↓␈αwhich␈αis␈αtrue␈αif␈αthe␈αfirst␈αargument␈αis␈αgreater␈αthat␈αthe␈αsecond␈α
can␈αbe
␈↓ ↓H␈↓computed by

␈↓ ↓H␈↓2.3)␈↓ α?␈↓↓greaterp[n,m] ← ␈↓αif␈↓↓ n = ␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ m = ␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓  greaterp[n-␈↓¬1␈↓↓,m-␈↓¬1␈↓↓]␈↓ 

␈↓ ↓H␈↓Here␈α
we␈αuse␈α
␈↓¬0␈α␈↓to␈α
represent␈α
␈↓αfalse␈↓␈αand␈α
␈↓¬1␈α␈↓to␈α
represent␈α␈↓αtrue␈↓␈α
in␈α
order␈αto␈α
keep␈αwithin␈α
the␈α
domain␈αof
␈↓ ↓H␈↓numbers.  We also write ␈↓↓n+␈↓¬1␈↓↓␈↓ instead of ␈↓↓add1 n␈↓ and ␈↓↓n-␈↓¬1␈↓↓␈↓ instead of ␈↓↓sub1 n␈↓.

␈↓ ↓H␈↓        We␈αcould␈αcontinue␈αalong␈αthese␈αlines␈αusing␈α␈↓↓plus␈↓␈αto␈αdefine␈α␈↓↓times,␈↓␈α␈↓↓times␈↓␈αto␈αdefine␈α␈↓↓exp,␈↓␈α␈↓↓differ␈↓
␈↓ ↓H␈↓to␈αdefine␈α␈↓↓quot␈↓␈αand␈α␈↓↓rem,␈↓␈αand␈αso␈αforth␈αbuilding␈αup␈αa␈αcollection␈αof␈αuseful␈αfunctions.␈α We␈αwill␈αleave
␈↓ ↓H␈↓this␈α∂as␈α∂an␈α⊂exercise␈α∂for␈α∂the␈α⊂reader.␈α∂ The␈α∂point␈α∂is␈α⊂that␈α∂at␈α∂each␈α⊂stage␈α∂the␈α∂recursive␈α⊂definition␈α∂is
␈↓ ↓H␈↓expressed␈α
in␈αterms␈α
of␈αgiven␈α
functions␈αin␈α
a␈α
very␈αsimple␈α
form.␈α Perhaps␈α
the␈αsimplest␈α
such␈α
form␈αis
␈↓ ↓H␈↓the following schema:

␈↓ ↓H␈↓2.4)␈↓ ∧;␈↓↓f[n] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬a ␈↓↓␈↓αelse␈↓↓ h[n-␈↓¬1␈↓↓,f[n-␈↓¬1␈↓↓]]␈↓. 

␈↓ ↓H␈↓Here␈α∞␈↓↓f␈↓␈α∞is␈α∞defined␈α∂in␈α∞terms␈α∞of␈α∞a␈α∂fixed␈α∞constant␈α∞␈↓¬a␈α∞␈↓and␈α∞a␈α∂given␈α∞function␈α∞␈↓↓h.␈↓␈α∞This␈α∂corresponds␈α∞to
␈↓ ↓H␈↓"primitive␈α
recursion"␈α
without␈α
parameters.␈α If␈α
we␈α
take␈α
 ␈↓↓h[k,m]=[k+␈↓¬1␈↓↓]␈α␈↓π*␈↓↓␈α
m␈↓␈α
and␈α
␈↓¬a␈α␈↓=␈α
␈↓¬1␈α
␈↓we␈α
have␈αthe
␈↓ ↓H␈↓definition of ␈↓↓fact␈↓ given above (1.1).

␈↓ ↓H␈↓If␈α
we␈α
allow␈α
parameters␈α
to␈α
be␈α
carried␈α
along␈α
we␈α
get␈α
the␈α
usual␈α
form␈α
of␈α
primitive␈α
recursion␈α(shown
␈↓ ↓H␈↓with only one parameter for simplicity)

␈↓ ↓H␈↓2.5)␈↓ βr␈↓↓f[n,m] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ g[m] ␈↓αelse␈↓↓ h[n-␈↓¬1␈↓↓,m,f[n-␈↓¬1␈↓↓,m]]␈↓ 

␈↓ ↓H␈↓The␈α
definition␈αof␈α
␈↓↓plus␈↓␈αgiven␈α
above␈αhas␈α
this␈αform␈α
with␈α␈↓↓g[m]=m␈↓␈α
and␈α␈↓↓h[n,m,k]=k+␈↓¬1␈↓↓␈↓.␈α
  As␈αa␈α
further
␈↓ ↓H␈↓generalization␈αwe␈αallow␈α
the␈αparametric␈αargument␈α
of␈α␈↓↓f␈↓␈α(in␈α
the␈αrecursive␈αcall)␈α
to␈αbe␈αa␈αgiven␈α
function
␈↓ ↓H␈↓of the parameter and the first argument giving

␈↓ ↓H␈↓2.6)␈↓ β?␈↓↓f[n,m] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ g[m] ␈↓αelse␈↓↓ h[n-␈↓¬1␈↓↓,m,f[n-␈↓¬1␈↓↓,j[n-␈↓¬1␈↓↓,m]]]␈↓. 

␈↓ ↓H␈↓The␈α∞definitions␈α∞of␈α
␈↓↓differ␈↓␈α∞and␈α∞␈↓↓greaterp␈↓␈α∞given␈α
above␈α∞has␈α∞this␈α
form.␈α∞ Also␈α∞the␈α∞alternate␈α
recursive
␈↓ ↓H␈↓definition␈αof␈αthe␈αfactorial␈αfunction␈α(1.3)␈αis␈αof␈αthis␈αform.␈α We␈αmay␈αwish␈αto␈αexpress␈αthe␈αcomputation
␈↓ ↓H␈↓directly␈αin␈αterms␈αof␈αseveral␈αpreceding␈αvalues␈αinstead␈αof␈αjust␈α␈↓↓f[n-␈↓¬1␈↓↓]␈↓.␈α  One␈αform␈αof␈αthis␈α"course␈αof
␈↓ ↓H␈↓values" type recursion is given by

␈↓ ↓H␈↓␈↓ ∧8␈↓↓f[n] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬a␈↓↓␈↓β0␈↓↓ ␈↓αelse␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓   ␈↓αif␈↓↓ n=␈↓¬1 ␈↓↓␈↓αthen␈↓↓ ␈↓¬a␈↓↓␈↓β1␈↓↓ ␈↓αelse␈↓↓␈↓
␈↓ ↓H␈↓2.7)␈↓ ¬8..........

␈↓ ↓H␈↓␈↓ ∧8␈↓↓   ␈↓αif␈↓↓ n=␈↓¬b ␈↓↓␈↓αthen␈↓↓ ␈↓¬a␈↓↓␈↓βb␈↓↓ ␈↓αelse␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓     h[n,f[p␈↓β1␈↓↓[n]],...f[[p␈↓βc␈↓↓[n]]]␈↓
␈↓ ↓H␈↓34␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓ where␈α∞␈↓↓␈↓¬0␈↓↓≤p␈↓βi␈↓↓[n]<n␈↓␈α∞for␈α
␈↓¬1≤i≤k␈α∞␈↓when␈α∞␈↓↓␈↓¬b␈↓↓<n␈↓.␈α∞ As␈α
an␈α∞example␈α∞we␈α∞have␈α
the␈α∞definition␈α∞of␈α∞the␈α
function
␈↓ ↓H␈↓giving the ␈↓↓n␈↓th Fibonacci number:

␈↓ ↓H␈↓2.8)␈↓ β⊗␈↓↓fib[n] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ n=␈↓¬1 ␈↓↓␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ fib[n-␈↓¬1␈↓↓] + fib[n-␈↓¬2␈↓↓]␈↓ 

␈↓ ↓H␈↓We␈α⊃note␈α⊃that␈α⊃this␈α⊃is␈α⊃a␈α⊃particularly␈α⊂unfavorable␈α⊃case␈α⊃for␈α⊃recursively␈α⊃defined␈α⊃functions␈α⊃as␈α⊂the
␈↓ ↓H␈↓evaluation␈α∂from␈α∂this␈α∂definition␈α∂takes␈α⊂order␈α∂of␈α∂␈↓↓fib n␈↓␈α∂amount␈α∂of␈α⊂work␈α∂for␈α∂input␈α∂of␈α∂␈↓↓n.␈↓␈α⊂ A␈α∂more
␈↓ ↓H␈↓efficient definition is:

␈↓ ↓H␈↓␈↓ ∧k␈↓↓fiba[n] ← fibb[n,␈↓¬0␈↓↓,␈↓¬1␈↓↓]                 ␈↓
␈↓ ↓H␈↓2.9)
␈↓ ↓H␈↓␈↓ βy␈↓↓fibb[n,k,m] ← ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ k ␈↓αelse␈↓↓ fibb[n-␈↓¬1␈↓↓,m,m+k]␈↓

␈↓ ↓H␈↓Evaluation␈αof␈αthe␈α␈↓↓n␈↓th␈αFibonacci␈αnumber␈αusing␈αthis␈αdefinition␈αrequires␈αonly␈αorder␈αof␈α␈↓↓n␈↓␈αamount␈αof
␈↓ ↓H␈↓work.

␈↓ ↓H␈↓        The␈α∞above␈α
forms␈α∞of␈α
recursive␈α∞function␈α
definition␈α∞all␈α
have␈α∞a␈α
very␈α∞nice␈α∞property.␈α
 Namely,
␈↓ ↓H␈↓assuming␈αthat␈αthe␈αgiven␈αfunctions␈αappearing␈αin␈αthe␈αschemas␈αare␈αtotal,␈αthe␈αfunction␈αdefined␈αby␈α
the
␈↓ ↓H␈↓schema␈αis␈α
total.␈α That␈αis␈α
the␈αrules␈α
given␈αfor␈αevaluation␈α
of␈αsuch␈αfunctions␈α
will␈αalways␈α
produce␈αan
␈↓ ↓H␈↓answer in a finite number of steps.  The general form of recursive definition

␈↓ ↓H␈↓2.10)␈↓ ∧y␈↓↓f[n1,...,nk] ← ␈↓πt␈↓↓[f,n1,...,nk]␈↓ 

␈↓ ↓H␈↓where␈α∂␈↓πt␈↓␈α∂is␈α⊂some␈α∂term␈α∂involving␈α⊂␈↓↓f,␈↓␈α∂the␈α∂arguments␈α∂␈↓↓ni␈↓␈α⊂and␈α∂perhaps␈α∂additional␈α⊂given␈α∂functions.
␈↓ ↓H␈↓Definitions of this general form are not guaranteed to define total functions.   For example

␈↓ ↓H␈↓2.11)␈↓ ¬g␈↓↓loop n ← loop n␈↓ 

␈↓ ↓H␈↓is␈αa␈α
perfectly␈αgood␈α
definition,␈αhowever␈αthe␈α
rules␈αfor␈α
computation␈αwill␈αnot␈α
produce␈αan␈α
answer␈αfor
␈↓ ↓H␈↓any␈α⊂value␈α⊂of␈α⊂␈↓↓n.␈↓␈α⊂ There␈α⊂are␈α⊂of␈α⊃course␈α⊂many␈α⊂cases␈α⊂where␈α⊂the␈α⊂function␈α⊂defined␈α⊂by␈α⊃a␈α⊂recursive
␈↓ ↓H␈↓definition is total, but where the definition does not fit any of the above patterns.

␈↓ ↓H␈↓        The␈α⊂standard␈α⊂example␈α⊂of␈α⊂a␈α⊂total␈α⊂function␈α⊂on␈α⊂natural␈α⊂numbers␈α⊂that␈α⊂is␈α⊂not␈α⊂definable␈α∂by
␈↓ ↓H␈↓primitive recursion is known as Ackerman's function and is defined by

␈↓ ↓H␈↓2.12)␈↓ αβ␈↓↓   ack[m,n] ← ␈↓αif␈↓↓ m=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ n+␈↓¬1 ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ n=␈↓¬0 ␈↓↓␈↓αthen␈↓↓ ack[m-␈↓¬1␈↓↓,␈↓¬1␈↓↓] ␈↓αelse␈↓↓ ack[m-␈↓¬1␈↓↓,ack[m,n-␈↓¬1␈↓↓]]␈↓. 

␈↓ ↓H␈↓If␈αyou␈αwork␈αout␈α
the␈αvalues␈αof␈α␈↓↓ack[m,n]␈↓␈α
for␈αincreasing␈αvalues␈αof␈α
the␈αarguments,␈αyou␈αwill␈α
see␈αthat
␈↓ ↓H␈↓the␈αamount␈αof␈αcomputation␈αgrows␈αvery␈αrapidly.␈α  This␈αis␈αrelated␈αto␈αthe␈αfact␈αthat␈αthe␈αfunction␈αcan
␈↓ ↓H␈↓not be defined by any collection of primitive recursion schemas.



␈↓ ↓H␈↓3.  ␈↓αSimple list recursion.␈↓


␈↓ ↓H␈↓        About␈α
the␈α
simplest␈α
form␈α
of␈α
recursion␈α
in␈α
LISP␈αoccurs␈α
when␈α
one␈α
of␈α
the␈α
arguments␈α
is␈α
a␈αlist,
␈↓ ↓H␈↓the␈α
result␈α
is␈αimmediate␈α
when␈α
the␈αargument␈α
is␈α
null,␈α
and␈αotherwise␈α
we␈α
need␈αonly␈α
know␈α
the␈αresult␈α
for
␈↓ ↓H␈↓the␈α∂d-part␈α∂of␈α∂that␈α∂argument.␈α∂ Several␈α∂of␈α∂the␈α∂functions␈α∂defined␈α∂in␈α∂Chapter␈α∂I␈α∂are␈α∂of␈α⊂this␈α∂form.
␈↓ ↓H␈↓Consider,␈α
for␈α
example,␈α
␈↓↓u*v␈↓,␈α
the␈α
result␈α
of␈α
␈↓↓append␈↓ing␈α
the␈α
list␈α
␈↓↓v␈↓␈α
to␈α
the␈α
list␈α
␈↓↓u.␈↓␈α
The␈α
result␈αis␈α
immediate
␈↓ ↓H␈↓for the case ␈↓αn|␈↓␈↓↓u␈↓ and otherwise depends on the result for ␈↓αd|␈↓␈↓↓u.␈↓  Thus, we have
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *35


␈↓ ↓H␈↓3.1)␈↓ ∧M␈↓↓u*v ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]␈↓. 

␈↓ ↓H␈↓Notice␈α
that␈α∞if␈α
we␈α∞had␈α
tried␈α
to␈α∞recur␈α
on␈α∞␈↓↓v␈↓␈α
rather␈α∞than␈α
on␈α
␈↓↓u␈↓␈α∞we␈α
would␈α∞not␈α
have␈α∞been␈α
successful.
␈↓ ↓H␈↓The␈α∞result␈α
would␈α∞be␈α∞immediate␈α
for␈α∞␈↓αn|␈↓␈↓↓v,␈↓␈α∞but␈α
␈↓↓u*v␈↓␈α∞cannot␈α
be␈α∞constructed␈α∞in␈α
any␈α∞direct␈α∞way␈α
from
␈↓ ↓H␈↓␈↓↓u*␈↓αd|␈↓↓v␈↓␈αwithout␈αa␈αfunction␈αthat␈αputs␈αan␈αelement␈αonto␈αthe␈αend␈αof␈αa␈αlist.␈α (From␈αa␈αstrictly␈αlist␈αpoint␈αof
␈↓ ↓H␈↓view,␈αsuch␈α
a␈αfunction␈αwould␈α
be␈αas␈αelementary␈α
as␈α␈↓↓cons␈↓␈αwhich␈α
puts␈αan␈αelement␈α
onto␈αthe␈αfront␈α
of␈αa
␈↓ ↓H␈↓list,␈αbut,␈α
when␈αwe␈α
consider␈αthe␈αimplementation␈α
of␈αlists␈α
by␈αlist␈αstructures,␈α
we␈αsee␈α
that␈αthe␈αfunction␈α
is
␈↓ ↓H␈↓not␈αso␈αelementary.␈α This␈αhas␈αled␈αsome␈αpeople␈αto␈αconstruct␈αsystems␈αin␈αwhich␈αlists␈αare␈αbi-directional,
␈↓ ↓H␈↓but,␈αin␈αthe␈α
main,␈αthis␈αhas␈α
turned␈αout␈αto␈α
be␈αa␈αbad␈αidea).␈α
 Anyway,␈αit␈αis␈α
usually␈αeasier␈αto␈α
recur␈αon
␈↓ ↓H␈↓one argument of a function than to recur on the others.

␈↓ ↓H␈↓Another␈α
example␈α
is␈αthe␈α
function␈α
␈↓↓member␈↓␈αthat␈α
tests␈α
for␈α
membership␈αin␈α
a␈α
list.␈α If␈α
the␈α
list␈α
is␈αempty
␈↓ ↓H␈↓then␈αthe␈αanswer␈αis␈α␈↓¬NIL␈↓␈αotherwise␈αeither␈αthe␈αelement␈αwe␈αare␈αsearching␈αfor␈αis␈αthe␈αfirst␈αthing␈αon␈αthe
␈↓ ↓H␈↓list, or it is in the rest of the list.  This reasoning gives rise to the recursive definition

␈↓ ↓H␈↓3.2)␈↓ βC␈↓↓member[x,u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ x = ␈↓αa|␈↓↓u ∨ member[x,␈↓αd|␈↓↓u]␈↓. 

␈↓ ↓H␈↓The␈α∞reader␈α∞should␈α∞observe␈α∞that␈α∞this␈α∞definition␈α∞is␈α∞equivalent␈α∞to␈α∞that␈α∞given␈α∞by␈α∞(I.8.6)␈α∞using␈α∞only
␈↓ ↓H␈↓logical connectives.

␈↓ ↓H␈↓        The function ␈↓↓reverse␈↓ is another example of simple list recursion.

␈↓ ↓H␈↓3.3)␈↓ βN␈↓↓reverse[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [reverse ␈↓αd|␈↓↓u] * [␈↓αa|␈↓↓u . ␈↓¬NIL␈↓↓]␈↓ 

␈↓ ↓H␈↓This␈α
straight␈α
forward␈α
definition␈α
of␈α
␈↓↓reverse␈↓␈α
 involves␈α␈↓↓append␈↓ing␈α
the␈α
␈↓↓reverse␈↓␈α
of␈α
the␈α
rest␈α
of␈α
the␈αlist␈α
to
␈↓ ↓H␈↓the list containing only the first element of the list.

␈↓ ↓H␈↓        As␈α∀with␈α∀recursive␈α∪definition␈α∀of␈α∀numerical␈α∪functions␈α∀we␈α∀see␈α∪that␈α∀simple␈α∀list␈α∪recursion
␈↓ ↓H␈↓parallels␈α
the␈α
description␈αof␈α
the␈α
domain␈αof␈α
lists␈α
which␈αsays␈α
that␈α
a␈α
list␈αis␈α
either␈α
␈↓¬NIL␈↓␈αor␈α
it␈α
is␈αthe␈α
result
␈↓ ↓H␈↓of␈α␈↓↓cons␈↓ing␈α
an␈αS-expression␈αonto␈α
a␈α"smaller"␈α
list.␈α  We␈αcan␈α
give␈αrecursion␈α
schemas␈αfor␈αlist␈α
recursion
␈↓ ↓H␈↓analagous␈αto␈αthose␈αfor␈αnumerical␈αfunctions.␈α  In␈αgeneral,␈αthe␈αrecursive␈αdefinition␈αof␈αa␈αfunction␈αon
␈↓ ↓H␈↓lists␈αmust␈αtake␈α
into␈αaccount␈αthe␈αfact␈α
that␈αwe␈αhave␈αnot␈α
one␈α"successor"␈αof␈αa␈α
given␈αlist,␈αbut␈α
one␈αfor
␈↓ ↓H␈↓each␈α∂possible␈α∂S-expression.␈α⊂ For␈α∂example␈α∂"primitive␈α∂list␈α⊂recursion"␈α∂without␈α∂parameters␈α⊂has␈α∂the
␈↓ ↓H␈↓form

␈↓ ↓H␈↓3.4)␈↓ ∧+␈↓↓f[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ g0 ␈↓αelse␈↓↓ h[␈↓αa|␈↓↓u, ␈↓αd|␈↓↓u, f[␈↓αd|␈↓↓u]]␈↓ 

␈↓ ↓H␈↓Taking␈αg0␈α=␈α␈↓¬NIL␈↓␈αand␈α␈↓↓h[x,v,w]=␈αw␈α*␈α[x␈α.␈α␈↓¬NIL␈↓↓]␈↓␈αwe␈αget␈αthe␈αdefinition␈αof␈α␈↓↓reverse␈↓␈αgiven␈αabove␈α(3.3).
␈↓ ↓H␈↓If we mix numerical and list computation  taking g0=␈↓¬0 ␈↓and ␈↓↓h[x,u,n]=n+␈↓¬1␈↓↓␈↓ we get

␈↓ ↓H␈↓3.5)␈↓ ∧-␈↓↓length[u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ length[␈↓αd|␈↓↓u]+␈↓¬1␈↓↓␈↓ 

␈↓ ↓H␈↓The function ␈↓↓last␈↓  which computes the last element of a list is another example.

␈↓ ↓H␈↓3.6)␈↓ ∧U␈↓↓last u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ ␈↓αa|␈↓↓u ␈↓αelse␈↓↓ last ␈↓αd|␈↓↓u␈↓, 

␈↓ ↓H␈↓In␈αorder␈αto␈αfit␈αthe␈αschema␈αexactly␈αthe␈αabove␈αdefinition␈αneeds␈αto␈αbe␈αpatched␈αsomewhat.␈α See␈αif␈αyou
␈↓ ↓H␈↓can figure out what must be done and what the "given" function ␈↓↓h␈↓ should be.

␈↓ ↓H␈↓The schema for primitive list recursion with parameters (only one parameter shown) is
␈↓ ↓H␈↓36␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓3.7)␈↓ β`␈↓↓f[u,x] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ g[x] ␈↓αelse␈↓↓  h[␈↓αa|␈↓↓u, ␈↓αd|␈↓↓u, x, f[␈↓αd|␈↓↓u, v]]␈↓. 

␈↓ ↓H␈↓␈↓↓append␈↓␈α
(3.1),␈α
␈↓↓member␈↓␈α
(3.2)␈α
and␈α
␈↓↓assoc␈↓␈α
(I.9.3)␈αare␈α
examples␈α
of␈α
this␈α
form␈α
of␈α
definition.␈α
  Allowing␈αa
␈↓ ↓H␈↓given␈αfunction␈α
of␈αthe␈αarguments␈α
to␈αoccur␈αin␈α
the␈αparameter␈αposition␈α
in␈αthe␈αrecursive␈α
call␈αon␈α
␈↓↓f␈↓␈αwe
␈↓ ↓H␈↓obtain the schema

␈↓ ↓H␈↓3.8)␈↓ β↔␈↓↓f[u,x] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ g[x] ␈↓αelse␈↓↓ h[␈↓αa|␈↓↓u, ␈↓αd|␈↓↓u, x, f[␈↓αd|␈↓↓u, j[␈↓αa|␈↓↓u, ␈↓αd|␈↓↓u, x]]]␈↓. 

␈↓ ↓H␈↓The␈α⊃definition␈α⊃of␈α⊃␈↓↓reverse␈↓␈α⊃(I.8.9)␈α⊃in␈α⊃terms␈α⊃of␈α⊃the␈α⊃basic␈α⊃␈↓↓cons␈↓␈α⊃operation,␈α⊃itself␈α⊃and␈α⊃an␈α⊂auxiliary
␈↓ ↓H␈↓variable has this form.

␈↓ ↓H␈↓        The␈α
simple␈α
recursion␈α
on␈α
lists␈α
without␈α
parameters␈α
simply␈α
proceeds␈α
through␈α
a␈α
list␈αgathering
␈↓ ↓H␈↓results␈α∞to␈α∂be␈α∞used␈α∞in␈α∂constructing␈α∞the␈α∞answer,␈α∂but␈α∞provides␈α∞no␈α∂access␈α∞to␈α∂information␈α∞previously
␈↓ ↓H␈↓obtained.␈α
 When␈αwe␈α
allow␈αparameters␈α
to␈α
be␈αcarried␈α
along␈αthe␈α
information␈α
can␈αbe␈α
passed␈αdown␈α
the
␈↓ ↓H␈↓list␈α∂as␈α∂well␈α∂as␈α∂results␈α∂being␈α∂passed␈α∂back␈α∂up.␈α∂ The␈α∂appropriate␈α∂mixing␈α∂of␈α∂control␈α∂structure␈α∞and
␈↓ ↓H␈↓information␈αpassing␈αis␈α
often␈αthe␈αkey␈α
to␈αsolving␈αa␈α
programming␈αproblem.␈α We␈α
shall␈αsee␈αin␈α
a␈αlater
␈↓ ↓H␈↓sections␈α∞how␈α∞one␈α
can␈α∞sometimes␈α∞solve␈α
the␈α∞two␈α∞aspects␈α
of␈α∞the␈α∞problem␈α
separately␈α∞and␈α∞then␈α
piece
␈↓ ↓H␈↓together the results.

␈↓ ↓H␈↓        The␈α
function␈α
␈↓↓alt␈↓␈α(I.8.1)␈α
which␈α
returns␈αa␈α
list␈α
of␈α
alternate␈αelements␈α
of␈α
a␈αlist␈α
is␈α
also␈α
based␈αon
␈↓ ↓H␈↓list␈αrecursion,␈α
however␈αthe␈αform␈α
of␈αthe␈α
recursion␈αis␈αdifferent.␈α
 Here␈αthe␈αbase␈α
cases␈αare␈α
the␈αempty
␈↓ ↓H␈↓list␈αand␈αthe␈αlist␈αcontaining␈αone␈αelement.␈α In␈αthe␈αgeneral␈αcase␈αwe␈αuse␈αthe␈αresult␈αfor␈α␈↓αdd|␈↓␈↓↓u␈↓␈αto␈αcompute
␈↓ ↓H␈↓the result for ␈↓↓u.␈↓  This is analogous to the "course of values" recursion on numbers (2.7).

␈↓ ↓H␈↓        The␈α∂above␈α∂schemas,␈α⊂like␈α∂their␈α∂numerical␈α∂counterparts␈α⊂yield␈α∂definitions␈α∂of␈α⊂total␈α∂functions
␈↓ ↓H␈↓(assuming␈αthat␈αthe␈αgiven␈αfunctions␈αare␈αtotal).␈α The␈αmost␈αgeneral␈αform␈αof␈αlist␈αrecursion␈αis␈αthe␈αsame
␈↓ ↓H␈↓as␈αthat␈αfor␈αnumerical␈αrecursion␈α(2.10).␈α In␈αthe␈αcase␈αof␈αpure␈αlist␈αrecursion␈αwe␈αbuild␈αthe␈αterm␈αon␈αthe
␈↓ ↓H␈↓right␈αhand␈α
side␈αfrom␈α
basic␈αlist␈α
functions␈αand␈α
expect␈αthe␈α
arguments␈αto␈α
be␈αlists.␈α
 As␈αwe␈α
have␈αseen
␈↓ ↓H␈↓above␈α
we␈α
often␈α
mix␈αcomputation␈α
involving␈α
lists,␈α
numbers␈αand␈α
S-expression␈α
in␈α
general.␈α
 Also,␈αas
␈↓ ↓H␈↓with␈αnumerical␈αrecursion,␈αrecursive␈αdefinition␈αof␈αfunctions␈αon␈αlists␈αdoes␈αnot␈αin␈αgeneral␈αgive␈αrise␈αto
␈↓ ↓H␈↓total␈αfunctions␈αand␈α
some␈αcare␈αmust␈α
be␈αused␈αto␈α
make␈αsure␈αyour␈α
program␈αwill␈αterminate.␈α
  We␈αwill
␈↓ ↓H␈↓have more to say about proving termination in Chapter III.



␈↓ ↓H␈↓4.  ␈↓αSimple S-expression recursion.␈↓


␈↓ ↓H␈↓        In␈αthe␈αcase␈αof␈αrecursion␈α
on␈αthe␈αstructure␈αof␈αS-expressions,␈α
the␈αsimplest␈αsituation␈αis␈αwhen␈α
the
␈↓ ↓H␈↓value␈αof␈αthe␈αfunction␈αis␈αimmediate␈αfor␈αatoms,␈αand␈αfor␈αnon-atoms␈αdepends␈αonly␈αon␈αthe␈αvalues␈αfor
␈↓ ↓H␈↓the a-part and the d-part of the argument.  Thus ␈↓↓subst␈↓ was defined by

␈↓ ↓H␈↓4.1)␈↓ α→␈↓↓subst[x,y,z] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓z ␈↓αthen␈↓↓ [␈↓αif␈↓↓ z ␈↓αeq␈↓↓ y ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ z] ␈↓αelse␈↓↓ subst[x,y,␈↓αa|␈↓↓z] . subst[x,y,␈↓αd|␈↓↓z]␈↓. 

␈↓ ↓H␈↓More generally we have recursive definitions of the form

␈↓ ↓H␈↓4.2)␈↓ βq␈↓↓f[x] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ g[x] ␈↓αelse␈↓↓ h[␈↓αa|␈↓↓x, ␈↓αd|␈↓↓x, f[␈↓αa|␈↓↓x], f[␈↓αd|␈↓↓x]]␈↓. 

␈↓ ↓H␈↓Here␈α⊂again␈α⊂the␈α⊂recursive␈α⊂definition␈α⊂of␈α⊂functions␈α⊂parallels␈α⊂the␈α⊂description␈α⊂of␈α⊂the␈α⊃domain.␈α⊂The
␈↓ ↓H␈↓definitions of ␈↓↓size␈↓ (the number of atoms in an S-expression) and ␈↓↓fringe␈↓
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *37


␈↓ ↓H␈↓4.3)␈↓ ∧'␈↓↓size[x] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ 1 ␈↓αelse␈↓↓ size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x␈↓ 

␈↓ ↓H␈↓4.4)␈↓ βl␈↓↓fringe x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <x> ␈↓αelse␈↓↓ fringe ␈↓αa|␈↓↓x * fringe ␈↓αd|␈↓↓x␈↓ 

␈↓ ↓H␈↓are further examples of this form of definition.

␈↓ ↓H␈↓        The S-expression analogue of (2.6) and (3.8) (primitive S-expression recursion) is

␈↓ ↓H␈↓4.5)␈↓ αl␈↓↓f[x,y] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ g[x,y] ␈↓αelse␈↓↓ h[␈↓αa|␈↓↓x,␈↓αd|␈↓↓x,y,f[␈↓αa|␈↓↓x,ja[x,y]],f[␈↓αd|␈↓↓x,jd[x,y]]]␈↓ 

␈↓ ↓H␈↓and the following definition of ␈↓↓equal␈↓ is of this form

␈↓ ↓H␈↓4.6)␈↓ ↓r␈↓↓   x=y ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ [␈↓αif␈↓↓ ␈↓αat|␈↓↓y ␈↓αthen␈↓↓ x ␈↓αeq␈↓↓ y ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓] ␈↓αelse␈↓↓  ␈↓αif␈↓↓ ␈↓αat|␈↓↓y ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓x=␈↓αa|␈↓↓y ∧ ␈↓αd|␈↓↓x=␈↓αd|␈↓↓y␈↓. 

␈↓ ↓H␈↓Note␈αthat␈αusing␈αthe␈αfact␈αthat␈α␈↓αeq␈↓␈αis␈αin␈αfact␈αdefined␈αon␈αnon-atoms␈αin␈αactual␈αLISP␈αimplementations
␈↓ ↓H␈↓and properties of the propositional functions, we can simplify the definition of ␈↓↓equal␈↓ to

␈↓ ↓H␈↓4.7)␈↓ βp␈↓↓x = y ← x ␈↓αeq␈↓↓ y ∨ [¬␈↓αat|␈↓↓x ∧ ¬␈↓αat|␈↓↓y ∧ ␈↓αa|␈↓↓x = ␈↓αa|␈↓↓y ∧ ␈↓αd|␈↓↓x = ␈↓αd|␈↓↓y]␈↓. 

␈↓ ↓H␈↓as was given in Chapter I.

␈↓ ↓H␈↓        Although␈αthe␈αabove␈αdefinition␈αof␈α␈↓↓equal␈↓␈αis␈αan␈αinstance␈αof␈αthe␈αschema␈α(4.5)␈αit␈αis␈αmore␈αnatural
␈↓ ↓H␈↓to␈α
think␈α
of␈α
it␈α
as␈α
a␈α
parallel␈α
recursion␈α
through␈α
the␈α
two␈α
arguments␈α
rather␈α
than␈α
thinking␈α
of␈α
one␈α
of
␈↓ ↓H␈↓them␈α∞as␈α∞a␈α∞parameter.␈α∞ This␈α∂could␈α∞of␈α∞course␈α∞be␈α∞elaborated␈α∂to␈α∞carry␈α∞along␈α∞parameters␈α∞as␈α∂well␈α∞as
␈↓ ↓H␈↓recurring␈α∂in␈α⊂parallel␈α∂through␈α⊂some␈α∂collection␈α⊂of␈α∂arguments.␈α∂In␈α⊂later␈α∂chapters␈α⊂we␈α∂will␈α⊂see␈α∂more
␈↓ ↓H␈↓examples␈α∩of␈α∩programs␈α∩that␈α∩recur␈α∩on␈α∪more␈α∩than␈α∩one␈α∩argument.␈α∩ (For␈α∩example␈α∪␈↓↓samefringe␈↓␈α∩().
␈↓ ↓H␈↓The␈αimportant␈αpoint␈αto␈αcheck␈αin␈αsuch␈αprograms␈αis␈αthat␈αthe␈αcollection␈αof␈αarguments␈αbeing␈αrecurred
␈↓ ↓H␈↓on is always "simpler" in recursive calls to the program being defined.

␈↓ ↓H␈↓        The definition of ␈↓↓flatten␈↓ is an example of a double recursion.

␈↓ ↓H␈↓␈↓ ∧t␈↓↓flatten[x] ← flat[x, ␈↓¬NIL␈↓↓]           ␈↓
␈↓ ↓H␈↓4.8)
␈↓ ↓H␈↓␈↓ βr␈↓↓flat[x,y] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x . y ␈↓αelse␈↓↓ flat[␈↓αa|␈↓↓x,flat[␈↓αd|␈↓↓x,y]]␈↓

␈↓ ↓H␈↓Although␈α␈↓↓flatten␈↓␈αcomputes␈α
the␈αsame␈αfunction␈α
as␈α␈↓↓fringe,␈↓␈αit␈αis␈α
more␈αefficient␈α because␈α
the␈α␈↓↓append␈↓
␈↓ ↓H␈↓operation␈α∞used␈α∞by␈α∂␈↓↓fringe␈↓␈α∞copies␈α∞unnecessarily.␈α∂  As␈α∞we␈α∞shall␈α∂see␈α∞in␈α∞Chapter␈α∂,␈α∞␈↓↓flatten␈↓␈α∞is␈α∂also␈α∞an
␈↓ ↓H␈↓improvement␈α
over␈α
␈↓↓fringe␈↓␈α
in␈α
that␈α
it␈α
is␈α
partially␈α
amenable␈α
to␈α
tail␈α
recursive␈α
evaluation.␈α
  The␈α
kind␈α
of
␈↓ ↓H␈↓double␈αrecursion␈αused␈αin␈α␈↓↓flatten␈↓␈αis␈αoften␈αuseful.␈αOne␈αway␈αof␈αthinking␈αof␈αthis␈αform␈αof␈αrecursion␈αis
␈↓ ↓H␈↓that␈α∞results␈α∞are␈α∞computed␈α∞from␈α∂one␈α∞side␈α∞of␈α∞the␈α∞S-expression␈α∞(the␈α∂␈↓αd␈↓␈α∞side␈α∞in␈α∞this␈α∞case)␈α∂and␈α∞then
␈↓ ↓H␈↓passed␈αalong␈αto␈αthe␈αother␈αside.␈α Thus␈αwe␈αobtain␈α
a␈αflow␈αof␈αinformation␈αfrom␈αside␈αto␈αside␈αas␈αwell␈α
as
␈↓ ↓H␈↓top␈αto␈αbottom.␈α The␈αtechnique␈αof␈αwriting␈αa␈αsimple␈αand␈αeasily␈αunderstandable␈α
recursive␈αdefinition
␈↓ ↓H␈↓and␈αthen␈α
later␈αoptimizing␈αby␈α
the␈αkind␈α
of␈αtransformation␈αmade␈α
in␈αgoing␈αfrom␈α
␈↓↓fringe␈↓␈αto␈α
␈↓↓flatten␈↓␈αis
␈↓ ↓H␈↓often useful.

␈↓ ↓H␈↓        Note␈α∂that␈α∞the␈α∂definition␈α∞of␈α∂␈↓↓flat␈↓␈α∞does␈α∂not␈α∞fit␈α∂the␈α∞general␈α∂primitive␈α∞recursive␈α∂schema␈α∞(4.5).
␈↓ ↓H␈↓Thus␈α⊃we␈α⊂have␈α⊃an␈α⊂example␈α⊃where␈α⊂one␈α⊃definition␈α⊂of␈α⊃a␈α⊂function␈α⊃clearly␈α⊂fits␈α⊃one␈α⊂of␈α⊃the␈α⊂simple
␈↓ ↓H␈↓recursion␈αschemas␈α
and␈αthus␈α
defines␈αa␈α
total␈αfunction,␈αwhile␈α
for␈αanother␈α
definition␈αof␈α
the␈αfunction
␈↓ ↓H␈↓this␈α∞fact␈α
is␈α∞not␈α
immediate.␈α∞ Of␈α
course␈α∞showing␈α
that␈α∞both␈α
definitions␈α∞compute␈α
the␈α∞same␈α
function
␈↓ ↓H␈↓also␈α∩requires␈α⊃some␈α∩work.␈α∩  We␈α⊃could␈α∩add␈α⊃to␈α∩our␈α∩list␈α⊃of␈α∩schemas␈α⊃some␈α∩which␈α∩allow␈α⊃multiple
␈↓ ↓H␈↓38␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓recursions␈α
such␈αas␈α
exhibited␈αby␈α
␈↓↓flatten.␈↓␈α However␈α
no␈αmatter␈α
how␈αmany␈α
such␈αforms␈α
for␈αdefining
␈↓ ↓H␈↓total␈αfunctions␈αwe␈αallow,␈αwe␈αwill␈αeventually␈αhave␈αto␈αresort␈αto␈αthe␈αgeneral␈αform␈α(2.10)␈αwhich␈αis␈αalso
␈↓ ↓H␈↓the general form for S-expression recursion.



␈↓ ↓H␈↓5.  ␈↓αOther structural recursions.␈↓


␈↓ ↓H␈↓        When␈α
S-expressions␈α
are␈α
used␈α
to␈α
represent␈α
a␈α
class␈α
of␈α
expressions␈α
that␈α
are␈αdefined␈α
inductively
␈↓ ↓H␈↓then␈α∞functions␈α∞of␈α∞these␈α∞expressions␈α∞often␈α∞have␈α∞a␈α∞recursive␈α∞form␈α∞closely␈α∞related␈α∞to␈α∞the␈α∞inductive
␈↓ ↓H␈↓definition␈α⊃of␈α⊃the␈α⊃expressions.␈α⊃ For␈α⊂example␈α⊃the␈α⊃arithmetic␈α⊃interpreter␈α⊃␈↓↓numval␈↓␈α⊃(I.9.4)␈α⊂computes
␈↓ ↓H␈↓directly␈αthe␈αvalue␈αfor␈αconstants␈αand␈αvariables␈αand␈αfor␈αsums␈αand␈αproducts␈αit␈αcomputes␈αthe␈αvalue␈α
in
␈↓ ↓H␈↓terms␈α∂of␈α∂the␈α∂values␈α⊂of␈α∂the␈α∂subexpressions␈α∂from␈α∂which␈α⊂the␈α∂sum␈α∂or␈α∂product␈α∂is␈α⊂constructd.␈α∂ The
␈↓ ↓H␈↓function␈α␈↓↓diff␈↓␈α(I.11.3)␈αwhich␈αsymbolically␈αdifferentiates␈αthe␈αsame␈αclass␈αof␈αexpressions␈αhas␈α
a␈αsimilar
␈↓ ↓H␈↓recursive␈α
structure␈αin␈α
that␈αit␈α
knows␈αthe␈α
answers␈αimmediately␈α
for␈αconstants␈α
and␈αvariables␈α
and␈αfor
␈↓ ↓H␈↓sums␈α
and␈α
products␈αit␈α
uses␈α
the␈αresults␈α
for␈α
subexpressions␈αto␈α
obtain␈α
the␈αfinal␈α
answer.␈α
 If␈αwe␈α
consider
␈↓ ↓H␈↓the␈α∂arithmetic␈α∞expressions␈α∂where␈α∂sum␈α∞and␈α∂product␈α∂are␈α∞simple␈α∂binary␈α∂operators␈α∞we␈α∂can␈α∂write␈α∞a
␈↓ ↓H␈↓simple recursion schema as follows:

␈↓ ↓H␈↓␈↓ αx␈↓↓f[x,y] ←␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ num[x] ␈↓αthen␈↓↓ gnum[x,y] ␈↓αelse␈↓↓␈↓
␈↓ ↓H␈↓5.1)␈↓ β8␈↓↓␈↓αif␈↓↓ isvar[x] ␈↓αthen␈↓↓ gvar[x,y] ␈↓αelse␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ issum[x] ␈↓αthen␈↓↓ gsum[s1 x, s2 x, y, f[s1 x, y], f[s2 x, y]] ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ispro[x] ␈↓αthen␈↓↓ gprod[p1 x, p2 x, y, f[p1 x, y], f[p2 x, y]]␈↓

␈↓ ↓H␈↓Then the evaluation function ␈↓↓numval␈↓

␈↓ ↓H␈↓␈↓ αx␈↓↓numval[x,y] ←␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ isnum[x] ␈↓αthen␈↓↓ cval[x] ␈↓αelse␈↓↓␈↓
␈↓ ↓H␈↓5.2)␈↓ β8␈↓↓␈↓αif␈↓↓ isvar[x] ␈↓αthen␈↓↓ lookup[x,y] ␈↓αelse␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ issum[x] ␈↓αthen␈↓↓ sum[numval[s1 x,y] numval[s2 x,y]] ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ isprod[x] ␈↓αthen␈↓↓ prod[numval[p1 x,y] numval[p2 x,y]] ␈↓

␈↓ ↓H␈↓is an instance of this schema where ␈↓↓cval,␈↓ ␈↓↓lookup,␈↓ ␈↓↓sum␈↓ and ␈↓↓prod␈↓ are given.

␈↓ ↓H␈↓        A␈α∪more␈α∪complicated␈α∪example␈α∪of␈α∀an␈α∪interpreter␈α∪with␈α∪recursive␈α∪structure␈α∪based␈α∀on␈α∪the
␈↓ ↓H␈↓expression language structure is the LISP interpreter ␈↓↓eval␈↓ (I.13.1).

␈↓ ↓H␈↓        In␈α
general␈α
if␈α
we␈α
have␈α
a␈α
domain␈α
defined␈α
by␈α
starting␈α
with␈α
a␈α
base␈α
set␈α
(with␈α
computable␈αtest␈α
for
␈↓ ↓H␈↓membership)␈α∩and␈α∪a␈α∩collection␈α∪constructors␈α∩with␈α∪which␈α∩we␈α∪build␈α∩elements␈α∪of␈α∩the␈α∪domain␈α∩by
␈↓ ↓H␈↓repeated␈αapplication␈α
starting␈αwith␈αelements␈α
of␈αthe␈αbase␈α
set,␈αthen␈αthere␈α
is␈αa␈α
corresponding␈αschema
␈↓ ↓H␈↓for␈αrecursive␈αdefinition␈αof␈αfunctions␈αon␈αthis␈αdomain.␈α  We␈αwill␈αsee␈αmore␈αexamples␈αand␈αhave␈αmore
␈↓ ↓H␈↓to say about this in later chapters.
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *39


␈↓ ↓H␈↓6.  ␈↓αGeneral tree recursion.␈↓


␈↓ ↓H␈↓        A␈αtree␈αstructure␈αis␈αeither␈αa␈αleaf␈αor␈αa␈αnode␈αtogether␈αwith␈αa␈αcollection␈αof␈αimmediate␈αsuccessors
␈↓ ↓H␈↓which␈α
are␈α
also␈α∞trees.␈α
 Many␈α
data␈α∞structures␈α
can␈α
be␈α
viewed␈α∞as␈α
trees.␈α
 For␈α∞example,␈α
S-expressions
␈↓ ↓H␈↓with␈αleaves␈αbeing␈αatoms␈αand␈αeach␈αnon-leaf␈αtree␈αhas␈αtwo␈αsuccessors,␈αgiven␈αby␈αthe␈α␈↓↓car␈↓␈αand␈αthe␈α␈↓↓cdr.␈↓
␈↓ ↓H␈↓More␈αgenerally␈αthe␈αstructures␈αdescribed␈αin␈αsection␈α␈↓π∞␈↓5␈α
can␈αbe␈αthought␈αof␈αas␈αtree␈αlike␈αstructures␈α
with
␈↓ ↓H␈↓the␈α∂basic␈α∞elements␈α∂as␈α∞leaves,␈α∂and␈α∂each␈α∞non-leaf␈α∂tree␈α∞having␈α∂immediate␈α∞successors␈α∂given␈α∂by␈α∞the
␈↓ ↓H␈↓selector␈α
functions␈α
for␈α
the␈α
particular␈α
type␈α
of␈α
node.␈α
 These␈α
structures␈α
all␈α
have␈α
two␈α
things␈α
in␈α
common.
␈↓ ↓H␈↓Namely,␈α(i)␈α
each␈αkind␈αof␈α
node␈αhas␈αa␈α
fixed␈αnumber␈α
of␈αsuccessors␈αand␈α
(ii)␈αwhen␈αdoing␈α
computations
␈↓ ↓H␈↓each such tree, is constructed from parts already present.

␈↓ ↓H␈↓        A␈αanother␈α
example␈αis␈α
a␈αtree␈α
 which␈αis␈αgiven␈α
by␈αan␈α
initial␈αposition␈α
together␈αwith␈αa␈α
␈↓↓successors␈↓
␈↓ ↓H␈↓function␈αfrom␈α
which␈αwe␈αmay␈α
obtain␈αthe␈αimmediate␈α
successors␈αof␈αany␈α
position␈α(of␈αrelevance).␈α
 Here
␈↓ ↓H␈↓a␈α∞node␈α
may␈α∞have␈α
an␈α∞arbitrary␈α
number␈α∞of␈α
successors␈α∞and␈α
parts␈α∞of␈α
the␈α∞trees␈α
are␈α∞built␈α∞as␈α
needed.
␈↓ ↓H␈↓Functions␈α⊂on␈α⊂such␈α⊂trees␈α⊂typically␈α⊂involve␈α⊂two␈α⊂processes,␈α⊂one␈α⊂for␈α⊂recognizing␈α⊂and␈α⊃dealing␈α⊂with
␈↓ ↓H␈↓leaves␈α⊂and␈α⊂one␈α⊃for␈α⊂processing␈α⊂a␈α⊃collection␈α⊂of␈α⊂succesors.␈α⊂ (Of␈α⊃course␈α⊂the␈α⊂two␈α⊃processes␈α⊂interact
␈↓ ↓H␈↓recursively.)

␈↓ ↓H␈↓        Consider␈α
the␈α
problem␈α
for␈αsearching␈α
a␈α
tree␈α
for␈αa␈α
position␈α
having␈α
some␈α
particular␈αproperty.
␈↓ ↓H␈↓We␈αcan␈αwrite␈αa␈αprogram␈αthat␈αdescribes␈αa␈αdepth␈αfirst␈α search␈αindependent␈αof␈αthe␈αproperty␈αor␈αkind
␈↓ ↓H␈↓of␈α∀tree.␈α∪ It␈α∀can␈α∀be␈α∪used␈α∀to␈α∪search␈α∀specific␈α∀trees␈α∪ by␈α∀defining␈α∪the␈α∀three␈α∀auxiliary␈α∪functions
␈↓ ↓H␈↓␈↓↓successors,␈↓ ␈↓↓ter,␈↓ and ␈↓↓lose␈↓ for the particular problem.  We have

␈↓ ↓H␈↓6.1)  ␈↓ αQ␈↓↓search p ← ␈↓αif␈↓↓ lose p ␈↓αthen␈↓↓ ␈↓¬LOSE ␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ter p ␈↓αthen␈↓↓ p ␈↓αelse␈↓↓ searchlis[successors p]␈↓ 

␈↓ ↓H␈↓where

␈↓ ↓H␈↓6.2) ␈↓ α	␈↓↓searchlis u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬LOSE ␈↓↓␈↓αelse␈↓↓ [λx: ␈↓αif␈↓↓ x = ␈↓¬LOSE ␈↓↓␈↓αthen␈↓↓ searchlis ␈↓αd|␈↓↓u ␈↓αelse␈↓↓ x][search ␈↓αa|␈↓↓u]␈↓. 

␈↓ ↓H␈↓In␈α
the␈α
applications,␈α
we␈α
start␈α
with␈αa␈α
position␈α
␈↓↓p0,␈↓␈α
and␈α
we␈α
are␈αlooking␈α
for␈α
a␈α
win␈α
in␈α
the␈αsuccessor␈α
tree
␈↓ ↓H␈↓of␈α
␈↓↓p0.␈↓␈α Certain␈α
positions␈αlose␈α
and␈α
there␈αis␈α
no␈αpoint␈α
looking␈α
at␈αtheir␈α
successors.␈α This␈α
is␈αdecided␈α
by
␈↓ ↓H␈↓the␈α
predicate␈α∞␈↓↓lose.␈↓␈α
 A␈α
position␈α∞is␈α
a␈α
win␈α∞if␈α
it␈α
doesn't␈α∞lose␈α
and␈α
it␈α∞satisfies␈α
the␈α
predicate␈α∞␈↓↓ter.␈↓␈α
 The
␈↓ ↓H␈↓successors␈α∞of␈α∞a␈α∞position␈α∂are␈α∞given␈α∞by␈α∞the␈α∂function␈α∞␈↓↓successors,␈↓␈α∞and␈α∞the␈α∂value␈α∞of␈α∞␈↓↓search␈α∞p␈↓␈α∂is␈α∞the
␈↓ ↓H␈↓winning␈αposition.␈α No␈α
non-losing␈αposition␈αshould␈αhave␈α
the␈αname␈α␈↓¬LOSE␈α
␈↓or␈αthe␈αfunction␈αwon't␈α
work
␈↓ ↓H␈↓properly.

␈↓ ↓H␈↓        One␈α∪application␈α∪is␈α∪finding␈α∪a␈α∩path␈α∪from␈α∪an␈α∪initial␈α∪node␈α∩to␈α∪a␈α∪final␈α∪node␈α∪in␈α∪a␈α∩graph
␈↓ ↓H␈↓represented␈α
by␈α
a␈α
list␈α
structure␈α
as␈α
described␈α
in␈α∞chapter␈α
I.␈α
 A␈α
position␈α
is␈α
a␈α
path␈α
starting␈α∞from␈α
the
␈↓ ↓H␈↓initial␈αnode␈αand␈αcontinuing␈αto␈αsome␈αintermediate␈αnode␈αand␈αis␈αrepresented␈αby␈αa␈αlist␈αof␈αits␈αnodes␈αin
␈↓ ↓H␈↓reverse order.  The three functions for this application are

␈↓ ↓H␈↓6.3)␈↓ βx␈↓↓lose p ← ␈↓αa|␈↓↓p ε ␈↓αd|␈↓↓p ,     ␈↓
␈↓ ↓H␈↓6.4)␈↓ βx␈↓↓ter p ← [␈↓αa|␈↓↓p = final] ,    ␈↓
␈↓ ↓H␈↓6.5)␈↓ βx␈↓↓successors p ← mapcar[λx: x . p, ␈↓αd|␈↓↓assoc[␈↓αa|␈↓↓p, graph]]␈↓.

␈↓ ↓H␈↓The␈αnode␈αwe␈αare␈αproposing␈αto␈αvisit␈αnext␈αloses␈αif␈αit␈αis␈αalready␈αin␈αthe␈αpath,␈αas␈αthat␈αmeans␈αwe␈αhave
␈↓ ↓H␈↓been␈α∞here␈α∞before.␈α∞ The␈α
successors␈α∞to␈α∞a␈α∞position␈α∞are␈α
all␈α∞of␈α∞those␈α∞positions␈α∞immediately␈α
reachable
␈↓ ↓H␈↓from␈α
the␈αcurrent␈α
node.␈α
 In␈αour␈α
list␈αnotation␈α
for␈α
a␈αgraph␈α
the␈α
immediately␈αreachable␈α
nodes␈αare␈α
those
␈↓ ↓H␈↓on␈α
the␈α
list␈α
associated␈α
with␈α
the␈α
current␈α
node␈α
in␈α
␈↓↓graph.␈↓␈α
 A␈α
position␈α
is␈α
terminal␈α
if␈α
the␈α
current␈α
node␈α
is
␈↓ ↓H␈↓the desired goal ␈↓↓final.␈↓
␈↓ ↓H␈↓40␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓        ␈↓↓search␈↓␈α∞is␈α∞used␈α
when␈α∞we␈α∞want␈α
to␈α∞search␈α∞a␈α∞tree␈α
of␈α∞possibilities␈α∞for␈α
a␈α∞solution␈α∞to␈α∞a␈α
problem.
␈↓ ↓H␈↓Naturally␈α∞we␈α∞can␈α∞do␈α∞other␈α∞things␈α∞with␈α∞tree␈α∞search␈α∞recursion␈α∞than␈α∞just␈α∞search.␈α∞ For␈α∞example␈α
we
␈↓ ↓H␈↓may␈α
want␈αto␈α
find␈αall␈α
solutions␈α
to␈αa␈α
problem.␈α This␈α
can␈αbe␈α
done␈α
with␈αa␈α
function␈α␈↓↓allsol␈↓␈α
that␈αuses␈α
the
␈↓ ↓H␈↓same ␈↓↓lose,␈↓ ␈↓↓ter,␈↓ and ␈↓↓successors␈↓ functions as does ␈↓↓search.␈↓ The simplest way to write ␈↓↓allsol␈↓ is

␈↓ ↓H␈↓6.6) ␈↓ α!␈↓↓allsol p ← ␈↓αif␈↓↓ lose p ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ter p ␈↓αthen␈↓↓ <p> ␈↓αelse␈↓↓ mapapp[allsol, successors p]␈↓, 

␈↓ ↓H␈↓where

␈↓ ↓H␈↓6.7) ␈↓ β≡␈↓↓mapapp[fn, u] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ fn[␈↓αa|␈↓↓u] * mappap[fn, ␈↓αd|␈↓↓u]␈↓. 

␈↓ ↓H␈↓This␈αform␈αof␈αthe␈αfunction␈αis␈αsomewhat␈αinefficient␈αbecause␈αof␈αall␈αthe␈α␈↓↓append␈↓ing.␈α A␈αmore␈αefficient
␈↓ ↓H␈↓form uses an auxiliary function as follows:

␈↓ ↓H␈↓␈↓ β8␈↓↓allsol p ← allsola[p, ␈↓¬NIL␈↓↓]␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓allsola[p, found] ← ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αif␈↓↓ lose p ␈↓αthen␈↓↓ found␈↓
␈↓ ↓H␈↓6.8)␈↓ βx␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ter p ␈↓αthen␈↓↓ p . found␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αelse␈↓↓ allsolb[successors p, found]␈↓,

␈↓ ↓H␈↓␈↓ β8␈↓↓allsolb[u, found] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ found ␈↓αelse␈↓↓ allsolb[␈↓αd|␈↓↓u, allsola[␈↓αa|␈↓↓u, found]]␈↓.

␈↓ ↓H␈↓The␈α⊃recursive␈α⊂program␈α⊃structure␈α⊂that␈α⊃arises␈α⊂here␈α⊃is␈α⊃common␈α⊂when␈α⊃a␈α⊂list␈α⊃is␈α⊂to␈α⊃be␈α⊃formed␈α⊂by
␈↓ ↓H␈↓recurring through a list structure.

␈↓ ↓H␈↓        We␈αwill␈αgive␈αfurther␈αapplications␈αof␈αand␈αvariations␈αon␈αthis␈αform␈αof␈αtree␈αsearch␈αrecursion␈αin
␈↓ ↓H␈↓Chapter .



␈↓ ↓H␈↓7.  ␈↓αSolving a LISP programming problem.␈↓


␈↓ ↓H␈↓        In␈αthis␈αsection␈αwe␈α
present␈αa␈αprogramming␈αproblem␈αthat␈α
is␈αsomewhat␈αmore␈αcomplex␈αthan␈α
the
␈↓ ↓H␈↓examples␈αof␈αearlier␈αsections␈α
and␈αwork␈αout␈αthe␈α
solution␈αin␈αdetail,␈αthe␈α
purpose␈αbeing␈αto␈αhelp␈αyou␈α
get
␈↓ ↓H␈↓started␈α∂thinking␈α∂recursively␈α∂and␈α⊂to␈α∂show␈α∂how␈α∂a␈α⊂typical␈α∂problem␈α∂might␈α∂be␈α⊂approached.␈α∂  After
␈↓ ↓H␈↓describing␈α
the␈α
function␈α∞we␈α
wish␈α
to␈α
compute,␈α∞we␈α
will␈α
write␈α
programs␈α∞for␈α
two␈α
simpler␈α∞but␈α
related
␈↓ ↓H␈↓functions␈α
in␈α
order␈α∞to␈α
better␈α
understand␈α∞various␈α
aspects␈α
of␈α∞the␈α
control␈α
structure␈α∞and␈α
information
␈↓ ↓H␈↓flow␈αneeded␈αin␈αthe␈αmain␈αcomputation.␈α This␈α
is␈αa␈αuseful␈αtechnique␈αin␈αproblem␈αsolving␈α
in␈αgeneral,
␈↓ ↓H␈↓the␈α
trick␈α
of␈α
course␈α
is␈α
to␈α
find␈α
a␈α
simplification␈α
which␈α
produces␈α
a␈α
problem␈α
that␈α
you␈α
can␈α
solve␈α
and
␈↓ ↓H␈↓whose␈α⊂solution␈α∂is␈α⊂useful␈α⊂in␈α∂solving␈α⊂the␈α∂original␈α⊂problem.␈α⊂  Having␈α∂written␈α⊂programs␈α⊂for␈α∂these
␈↓ ↓H␈↓simpler functions, we then use the insight gained to to solve the main problem.

␈↓ ↓H␈↓        The␈αproblem␈αhas␈αto␈α
do␈αwith␈α"lists␈αof␈αlists"␈α
or␈αL-lists.␈α An␈αL-list␈α
is␈αeither␈αthe␈αempty␈αlist,␈α
␈↓¬NIL␈↓,
␈↓ ↓H␈↓or␈α∞and␈α
atom␈α∞␈↓↓cons␈↓ed␈α
onto␈α∞the␈α
front␈α∞of␈α
an␈α∞L-list,␈α
or␈α∞an␈α
L-list␈α∞␈↓↓cons␈↓ed␈α
onto␈α∞the␈α
front␈α∞of␈α∞an␈α
L-list.
␈↓ ↓H␈↓Thus␈α∀␈↓¬(A (A B) C)␈↓␈α∃is␈α∀and␈α∃L-list,␈α∀but␈α∃␈↓¬(A (B . C) D)␈↓␈α∀is␈α∃not.␈α∀  Although␈α∃this␈α∀class␈α∃of␈α∀S-
␈↓ ↓H␈↓expressions␈α∃is␈α∃less␈α∃general␈α∃than␈α∃the␈α∃usual␈α∃LISP␈α∃notion␈α∃of␈α∃list,␈α∃it␈α∃is␈α∃more␈α∃uniform.␈α∃ The
␈↓ ↓H␈↓computation␈α
of␈α
a␈α
function␈α
on␈α
L-lists␈α
may␈α
use␈α
the␈α
value␈α
of␈α
the␈α
function␈α
on␈α
the␈α
␈↓↓car␈↓␈α
of␈α∞the␈α
L-list
␈↓ ↓H␈↓(when it is non-atomic) as well as on the ␈↓↓cdr␈↓ as both are again L-lists.
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *41


␈↓ ↓H␈↓        The␈α
function␈α∞␈↓↓allsubsub␈↓␈α
returns␈α
a␈α∞list␈α
of␈α∞all␈α
occurrences␈α
of␈α∞an␈α
L-list␈α∞␈↓↓u␈↓␈α
as␈α
a␈α∞sublist␈α
or␈α∞as␈α
a
␈↓ ↓H␈↓sublist␈αof␈αa␈αsublist␈αof␈αan␈αL-list␈α␈↓↓v␈↓␈α.␈α For␈αexample␈α␈↓¬(A␈αA)␈↓␈αoccurs␈αthree␈αtimes␈αas␈αa␈αsubsublist␈αof␈α␈↓¬(A␈αA
␈↓ ↓H␈↓¬A B A A)␈↓ and ␈↓¬(A)␈↓ occurs 4 times in ␈↓¬(A (B (A A)) (C (((A)))))␈↓.

␈↓ ↓H␈↓        Suppose␈α
we␈α
restrict␈α
the␈α
function␈α
to␈α
lists␈α∞of␈α
atoms.␈α
 Thus␈α
we␈α
wish␈α
to␈α
compute␈α∞the␈α
function
␈↓ ↓H␈↓␈↓↓allsub␈↓␈αthat␈αreturns␈αa␈αlist␈αof␈αall␈αoccurrences␈αof␈αa␈α
list␈αof␈αatoms␈α␈↓↓u␈↓␈αas␈αa␈αsublist␈αof␈αanother␈αlist␈αof␈α
atoms
␈↓ ↓H␈↓␈↓↓v.␈↓␈α
 The␈αfirst␈α
thing␈αto␈α
do␈α
is␈αdecide␈α
on␈αthe␈α
representation␈α
of␈αan␈α
occurrence␈αof␈α
one␈α
list␈αas␈α
a␈αsublist␈α
of
␈↓ ↓H␈↓another␈αlist.␈α In␈αthe␈αcase␈αof␈αlists␈αof␈αatoms,␈αa␈αfairly␈αnatural␈αsolution␈αis␈αto␈αrepresent␈αan␈αoccurrence␈α
of
␈↓ ↓H␈↓a␈αparticular␈αsublist␈αas␈αthe␈αnumber␈α␈↓↓n␈↓␈αcorresponding␈αto␈αposition␈αin␈αthe␈αlist␈αof␈αthe␈αbeginning␈αof␈αthat
␈↓ ↓H␈↓occurrence.  Thus

␈↓ ↓H␈↓␈↓ ∧0␈↓↓allsub[␈↓¬(A A),(A A A B A A)␈↓↓] = ␈↓¬(1 2 5)␈↓↓.␈↓ 

␈↓ ↓H␈↓        To␈α
compute␈α
␈↓↓allsub[u,v],␈↓␈α∞if␈α
␈↓↓v␈↓␈α
is␈α
␈↓¬NIL␈↓␈α∞then␈α
the␈α
answer␈α
is␈α∞␈↓¬NIL␈↓.␈α
 Otherwise,␈α
imagine␈α∞that␈α
we
␈↓ ↓H␈↓know␈αthe␈αanswer␈αfor␈α␈↓↓␈↓αd|␈↓↓v␈↓.␈α Then␈αthere␈αare␈αtwo␈αpossiblities.␈α Either␈α␈↓↓u␈↓␈α"matches"␈α␈↓↓v␈↓␈α(␈↓↓v␈↓␈αmay␈αbe␈αlonger
␈↓ ↓H␈↓than␈α
␈↓↓u,␈↓␈α
but␈α
up␈α
to␈α
the␈α
end␈α
of␈α
␈↓↓u␈↓␈α
the␈α
elements␈αof␈α
␈↓↓v␈↓␈α
and␈α
␈↓↓u␈↓␈α
are␈α
the␈α
same),␈α
or␈α
not.␈α
 If␈α
not,␈α
the␈αanswer␈α
is
␈↓ ↓H␈↓just␈α
the␈α∞result␈α
for␈α∞␈↓↓␈↓αd|␈↓↓v␈↓␈α
otherwise␈α∞we␈α
add␈α∞the␈α
current␈α∞position␈α
to␈α∞the␈α
result␈α∞for␈α
␈↓↓␈↓αd|␈↓↓v␈↓.␈α∞ This␈α
analysis
␈↓ ↓H␈↓suggests␈αthat␈αwe␈αwill␈αneed␈αan␈αadditional␈αargument␈αto␈αkeep␈αtrack␈αof␈αthe␈αcurrent␈αposition.␈α Thus␈αwe
␈↓ ↓H␈↓define␈αa␈αfunction␈α␈↓↓allsub1[u,v,p]␈↓␈αwhere␈αthe␈αargument␈α␈↓↓p␈↓␈αis␈αintended␈αto␈αcorrespond␈αto␈αthe␈αposition
␈↓ ↓H␈↓of␈α∞the␈α∞argument␈α
␈↓↓v␈↓␈α∞in␈α∞the␈α∞initial␈α
list.␈α∞ If␈α∞␈↓↓v␈↓␈α∞is␈α
␈↓¬NIL␈↓␈α∞then␈α∞the␈α∞result␈α
is␈α∞␈↓¬NIL␈↓.␈α∞ Otherwise␈α∞suppose␈α
we
␈↓ ↓H␈↓know␈α∀the␈α∀value␈α∀of␈α∀␈↓↓allsub1[u,␈↓αd|␈↓↓v,p+1]␈↓␈α∪then␈α∀we␈α∀either␈α∀add␈α∀␈↓↓p␈↓␈α∪onto␈α∀that␈α∀value␈α∀or␈α∀return␈α∪it
␈↓ ↓H␈↓unchanged␈αaccording␈αto␈αwhether␈αor␈αnot␈α␈↓↓u␈↓␈αmatches␈α␈↓↓v.␈↓␈α Assuming␈αwe␈αhave␈αa␈αsuitable␈αdefinition␈αof
␈↓ ↓H␈↓␈↓↓match,␈↓ the above analysis leads to the following definitions:

␈↓ ↓H␈↓␈↓ αx␈↓↓allsub[u, v] ← allsub1[u, v, 1]␈↓
␈↓ ↓H␈↓7.1)
␈↓ ↓H␈↓␈↓ αx␈↓↓allsub1[u, v, p] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓v ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ match[u, v] ␈↓αthen␈↓↓ p . allsub1[u, ␈↓αd|␈↓↓v, ␈↓¬1 ␈↓↓+ p]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ allsub1[u, ␈↓αd|␈↓↓v, ␈↓¬1 ␈↓↓+ p]␈↓.

␈↓ ↓H␈↓        It␈αremains␈α
for␈αus␈α
to␈αwrite␈αthe␈α
definition␈αof␈α
␈↓↓match[u,v]␈↓␈αwhich␈αdetermines␈α
whether␈αor␈α
not␈α␈↓↓u␈↓
␈↓ ↓H␈↓matches␈αthe␈αbeginning␈αof␈α␈↓↓v.␈↓␈α This␈αis␈αeasy.␈α If␈α␈↓↓u␈↓␈αis␈α␈↓¬NIL␈↓␈αthen␈αwe␈αhave␈αgotten␈αto␈αthe␈αend␈αof␈α␈↓↓u␈↓␈αwith
␈↓ ↓H␈↓out␈αfinding␈αa␈α
mismatch␈αso␈αwe␈α
return␈α␈↓¬T␈↓.␈α  If␈α
␈↓↓v␈↓␈αis␈α␈↓¬NIL␈↓␈α
(and␈α␈↓↓u␈↓␈αis␈α
not)␈αthen␈αwe␈α
return␈α␈↓¬NIL␈↓␈αand␈α␈↓↓u␈↓␈α
does
␈↓ ↓H␈↓not␈αmatch␈αthe␈αbeginning␈αof␈α␈↓↓v.␈↓␈α Otherwise␈αthe␈αresult␈αis␈α␈↓¬T␈↓␈αonly␈αif␈α␈↓↓␈↓αa|␈↓↓u = ␈↓αa|␈↓↓v␈↓␈αand␈αthe␈α␈↓↓cdr␈↓s␈αof␈α␈↓↓u␈↓␈αand␈α␈↓↓v␈↓
␈↓ ↓H␈↓match.  Thus

␈↓ ↓H␈↓7.2)␈↓ α;␈↓↓match[u, v] ←   ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬T␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓v ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u = ␈↓αa|␈↓↓v ∧ match[␈↓αd|␈↓↓u, ␈↓αd|␈↓↓v]␈↓. 

␈↓ ↓H␈↓The internal forms of the above definitions are

␈↓ ↓H␈↓¬        (DEFUN ALLSUB (U V) (ALLSUB1 U V 1))

␈↓ ↓H␈↓¬        (DEFUN ALLSUB1 (U V P)
␈↓ ↓H␈↓¬               (COND ((NULL V) NIL)
␈↓ ↓H␈↓¬                     ((MATCH U V) (CONS P (ALLSUB1 U (CDR V) (ADD1 P))))
␈↓ ↓H␈↓¬                     (T (ALLSUB1 U (CDR V) (ADD1 P)))))
␈↓ ↓H␈↓42␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓¬        (DEFUN MATCH (U V)
␈↓ ↓H␈↓¬               (COND ((NULL U) T)
␈↓ ↓H␈↓¬                     ((NULL V) NIL)
␈↓ ↓H␈↓¬                     (T (AND (EQUAL (CAR U) (CAR V))
␈↓ ↓H␈↓¬                             (MATCH (CDR U) (CDR V))))))

␈↓ ↓H␈↓        Now␈αwe␈α
return␈αto␈αthe␈α
␈↓↓allsubsub␈↓␈αproblem.␈αAs␈α
with␈αthe␈α
simpler␈αcase,␈αwe␈α
must␈αfirst␈αdecide␈α
how
␈↓ ↓H␈↓to␈αrepresent␈αan␈αoccurrence␈αof␈αan␈αL-list␈αas␈α
a␈αsubsublist␈αof␈αan␈αL-list.␈α Again␈αwe␈αneed␈αonly␈α
represent
␈↓ ↓H␈↓the␈αposition␈αwhere␈αthe␈αlist␈αmatch␈αbegins.␈α Consider␈αan␈αarbitrary␈αpoint,␈α␈↓↓p,␈↓␈α in␈αan␈αL-list.␈α Either␈αit
␈↓ ↓H␈↓is␈α
an␈α
element␈α
of␈α
the␈α
list,␈α∞or␈α
is␈α
contained␈α
in␈α
one␈α
of␈α
the␈α∞elements␈α
of␈α
the␈α
list.␈α
 In␈α
the␈α
first␈α∞case␈α
the
␈↓ ↓H␈↓position␈α
␈↓↓n␈↓␈α
of␈α
the␈α
element␈α
is␈α
sufficient,␈α
in␈α
the␈α
second␈α
case␈α
we␈α
need␈α
the␈α
position␈α
␈↓↓n␈↓␈α
of␈α∞the␈α
element
␈↓ ↓H␈↓together␈αwith␈αthe␈αposition␈αof␈αthe␈αpoint␈α␈↓↓p␈↓␈αrelative␈αto␈αthat␈αelement.␈α Thus␈αin␈αthe␈αfirst␈αcase␈α
we␈αtake
␈↓ ↓H␈↓the␈αlist␈αcontaining␈αonly␈αthe␈αnumber␈α␈↓↓n␈↓␈αand␈αin␈αthe␈αsecond␈αcase␈αwe␈αadd␈α␈↓↓n␈↓␈αonto␈αthe␈αlist␈αrepresenting
␈↓ ↓H␈↓the position of ␈↓↓p␈↓ in the ␈↓↓n␈↓th element of the list.  Using this representation we have

␈↓ ↓H␈↓␈↓ ↓z␈↓↓allsubsub[␈↓¬(A),(A (B (A A)) (C (((A)))))␈↓↓] = ␈↓¬((1) (2 2 1) (2 2 2) (3 2 1 1 1))␈↓↓␈↓ . 

␈↓ ↓H␈↓        To␈α
construct␈α∞the␈α
desired␈α∞list␈α
of␈α
positions␈α∞we␈α
check␈α∞for␈α
a␈α
match␈α∞of␈α
␈↓↓u␈↓␈α∞at␈α
each␈α∞position␈α
and
␈↓ ↓H␈↓add␈αmatching␈αpositions␈α
to␈αthe␈αresult.␈α
  (Actually␈αwe␈αwill␈α
see␈αthat␈αsome␈α
positions␈αcan␈αbe␈α
ruled␈αout
␈↓ ↓H␈↓with␈α∞out␈α∞explicitly␈α∞checking␈α∞for␈α∞a␈α∞match.)␈α
 In␈α∞particular,␈α∞we␈α∞will␈α∞need␈α∞to␈α∞pass␈α∞around␈α
sufficient
␈↓ ↓H␈↓information␈α
to␈αbe␈α
able␈αto␈α
construct␈αa␈α
representation␈αof␈α
the␈αcurrent␈α
position␈αwhenever␈α
a␈α
match␈αis
␈↓ ↓H␈↓found.␈α Let␈αus␈α
consider␈αthe␈αsimpler␈αproblem␈α
of␈αcomputing␈α␈↓↓allpos[v],␈↓␈αa␈α
list␈αof␈αall␈αthe␈α
positions␈αin
␈↓ ↓H␈↓␈↓↓v.␈↓␈α
 A␈αprogram␈α
for␈αthis␈α
 can␈αeasily␈α
be␈αmodified␈α
to␈αlist␈α
only␈αthose␈α
positions␈αsatisfying␈α
the␈α"match"
␈↓ ↓H␈↓condition.␈α∂ A␈α⊂definition␈α∂of␈α∂␈↓↓allpos␈↓␈α⊂can␈α∂be␈α∂obtained␈α⊂by␈α∂recalling␈α∂the␈α⊂discussion␈α∂about␈α∂of␈α⊂how␈α∂a
␈↓ ↓H␈↓position␈αis␈αto␈αbe␈αrepresented.␈α Namely␈αif␈α␈↓↓n␈↓␈αis␈αthe␈αposition␈αof␈α␈↓↓v␈↓␈αin␈αthe␈αlist␈αof␈αwhich␈α␈↓↓v␈↓␈αis␈αa␈αtail␈αthen
␈↓ ↓H␈↓(i)␈αif␈α␈↓↓v␈↓␈αis␈α␈↓¬NIL␈↓␈αthen␈αthere␈αare␈αno␈αpositions␈αand␈αthe␈αanswer␈αis␈α␈↓¬NIL␈↓,␈α (ii)␈α␈↓↓␈↓αa|␈↓↓v␈↓␈αis␈αan␈αatom␈αthen␈αadd␈α␈↓↓<n>␈↓
␈↓ ↓H␈↓to␈α
the␈α
list␈αof␈α
positions␈α
in␈α␈↓↓␈↓αd|␈↓↓v␈↓␈α
passing␈α
␈↓↓n+1␈↓␈αas␈α
the␈α
current␈αposition␈α
number␈α
(iii)␈α
otherwise␈αcompute
␈↓ ↓H␈↓the␈αlist␈αof␈αpositions␈αin␈α␈↓↓␈↓αa|␈↓↓v␈↓␈α(relative␈αto␈α␈↓↓␈↓αa|␈↓↓v␈↓),␈αtack␈α␈↓↓n␈↓␈αonto␈αthe␈αfront␈αof␈αeach␈αposition,␈αappend␈αthis␈α
onto
␈↓ ↓H␈↓the␈α
list␈α
of␈α
positions␈α
in␈α
␈↓↓␈↓αd|␈↓↓v␈↓␈α
(again␈α
passing␈α
␈↓↓n+1)␈↓␈α
and␈α
finally␈α
add␈α
the␈α
current␈α
position,␈α
␈↓↓<n>,␈↓␈α
to␈α
the
␈↓ ↓H␈↓result.   Thus we have

␈↓ ↓H␈↓␈↓ αx␈↓↓allpos v ← allpos1[v, 1]␈↓
␈↓ ↓H␈↓7.3)
␈↓ ↓H␈↓␈↓ αx␈↓↓allpos1[v, n] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓v ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓v ␈↓αthen␈↓↓ <n> . allpos1[␈↓αd|␈↓↓v, ␈↓¬1 ␈↓↓+ n]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ <n> . [tack[n, allpos ␈↓αa|␈↓↓v] * allpos1[␈↓αd|␈↓↓v, ␈↓¬1 ␈↓↓+ n]]␈↓

␈↓ ↓H␈↓7.4)␈↓ αx␈↓↓tack[n, w] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [n . ␈↓αa|␈↓↓w] . tack[n, ␈↓αd|␈↓↓w]␈↓

␈↓ ↓H␈↓Now␈α
we␈α
modify␈α
to␈α
␈↓↓allpos␈↓␈α
and␈α
␈↓↓allpos1␈↓␈α
to␈α
carry␈α
around␈α
the␈α
parameter␈α
␈↓↓u␈↓␈α
and␈α
rule␈α
out␈α
those␈α
positions
␈↓ ↓H␈↓that␈αdon't␈αmatch.␈α In␈αparticular␈αwe␈αonly␈αadd␈α␈↓↓<n>␈↓␈αto␈αthe␈αlist␈αif␈α␈↓↓match[u,v]␈↓␈αis␈αsatisfied,␈αand␈αin␈αthis
␈↓ ↓H␈↓case␈αwe␈αdon't␈αlook␈αfor␈αa␈αmatch␈αin␈α␈↓↓␈↓αa|␈↓↓v␈↓␈αas␈αthe␈αtop␈αlevel␈αmatch␈αmakes␈αthis␈αimpossible␈α(recall␈αour␈αlists
␈↓ ↓H␈↓are finite tree-like structures).  So we arrive at the following:
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *43


␈↓ ↓H␈↓␈↓ αx␈↓↓allsubsub[u, v] ← allsubsub1[u, v, 1]␈↓
␈↓ ↓H␈↓7.5)
␈↓ ↓H␈↓␈↓ αx␈↓↓allsubsub1[u, v, n] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓v ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ match[u, v] ␈↓αthen␈↓↓ <n> . allsubsub1[u, ␈↓αd|␈↓↓v, ␈↓¬1 ␈↓↓+ n]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓v ␈↓αthen␈↓↓ allsubsub1[u, ␈↓αd|␈↓↓v, ␈↓¬1 ␈↓↓+ n]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ tack[n, allsubsub[u, ␈↓αa|␈↓↓v]] * allsubsub1[u, ␈↓αd|␈↓↓v, ␈↓¬1 ␈↓↓+ n]␈↓

␈↓ ↓H␈↓An␈α⊃alternate␈α⊃solution␈α∩for␈α⊃the␈α⊃␈↓↓allpos␈↓␈α⊃and␈α∩the␈α⊃␈↓↓allsubsub␈↓␈α⊃problems␈α⊃is␈α∩to␈α⊃pass␈α⊃along␈α∩a␈α⊃complete
␈↓ ↓H␈↓representation␈αof␈αthe␈αcurrent␈αposition.␈α When␈αwe␈αpass␈α
it␈αin␈αthe␈α␈↓↓␈↓αd|␈↓↓v␈↓␈αdirection␈αwe␈αneed␈αto␈αadd␈α
1␈αto
␈↓ ↓H␈↓the␈αlast␈αelement␈α
and␈αwhen␈αwe␈αpass␈α
it␈αin␈αthe␈α␈↓↓␈↓αa|␈↓↓v␈↓␈α
direction␈αwe␈αneed␈αto␈α
add␈αa␈α1␈αto␈α
the␈αend␈αof␈αthe␈α
list.
␈↓ ↓H␈↓Since␈αall␈αof␈αthe␈αaction␈αis␈αat␈αthe␈αend␈αof␈αthe␈αlist␈αit␈αis␈αeasier␈αto␈αpass␈αalong␈αthe␈αreverse␈αof␈αthe␈αposition
␈↓ ↓H␈↓list␈α⊃and␈α⊃re-reverse␈α⊃it␈α⊃when␈α⊂returning␈α⊃the␈α⊃position␈α⊃as␈α⊃an␈α⊂answer.␈α⊃ Thus␈α⊃we␈α⊃get␈α⊃the␈α⊂following
␈↓ ↓H␈↓definitions

␈↓ ↓H␈↓␈↓ αx␈↓↓allpos v ← allpos1[v, ␈↓¬(1)␈↓↓]␈↓
␈↓ ↓H␈↓7.6)
␈↓ ↓H␈↓␈↓ αx␈↓↓allpos1[v, p] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓v ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓v ␈↓αthen␈↓↓ reverse p . allpos1[␈↓αd|␈↓↓v, [␈↓¬1 ␈↓↓+ ␈↓αa|␈↓↓p] . ␈↓αd|␈↓↓p]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ reverse p . [allpos1[␈↓αa|␈↓↓v, 1 . p]] * allpos1[␈↓αd|␈↓↓v, [␈↓¬1 ␈↓↓+ ␈↓αa|␈↓↓p] . ␈↓αd|␈↓↓p]]␈↓

␈↓ ↓H␈↓␈↓ αx␈↓↓allsubsub[u, v] ← allsubsub1[u, v, ␈↓¬(1)␈↓↓]␈↓
␈↓ ↓H␈↓7.7)
␈↓ ↓H␈↓␈↓ αx␈↓↓allsubsub1[u, v, p] ← ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓v ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ match[u, v] ␈↓αthen␈↓↓ reverse p . allsubsub1[u, ␈↓αd|␈↓↓v, [␈↓¬1 ␈↓↓+ ␈↓αa|␈↓↓p] . ␈↓αd|␈↓↓p]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓v ␈↓αthen␈↓↓ allsubsub1[u, ␈↓αd|␈↓↓v, [␈↓¬1 ␈↓↓+ ␈↓αa|␈↓↓p] . ␈↓αd|␈↓↓p]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αelse␈↓↓ allsubsub1[u, ␈↓αa|␈↓↓v, ␈↓¬1 ␈↓↓. p]] * allsubsub1[u, ␈↓αd|␈↓↓v, [␈↓¬1 ␈↓↓+ ␈↓αa|␈↓↓p] . ␈↓αd|␈↓↓p]␈↓

␈↓ ↓H␈↓Notice␈αthat␈αin␈αthe␈αfirst␈αsolution,␈αthe␈α
construction␈αof␈αthe␈αposition␈αrepresentation␈αwas␈αtaken␈α
care␈αof
␈↓ ↓H␈↓somewhat␈α∞automatically␈α∞by␈α∞the␈α∞recursive␈α∞structure␈α∂of␈α∞the␈α∞program,␈α∞while␈α∞in␈α∞the␈α∞second␈α∂case␈α∞we
␈↓ ↓H␈↓had to worry about the details of constructing the position representations.

␈↓ ↓H␈↓In␈αboth␈α
cases␈αthe␈αcompution␈α
works␈αby␈α
passing␈αinformation␈αboth␈α
down␈αand␈α
across␈αthe␈αlist␈α
structure
␈↓ ↓H␈↓and␈αgetting␈αresults␈αback.␈α What␈α
is␈αpassed␈αon␈αand␈αthe␈α
interpretation␈αof␈αthe␈αresult␈αreturned␈α
depends
␈↓ ↓H␈↓on␈α≠the␈α≠direction.␈α≠In␈α~this␈α≠example␈α≠there␈α≠is␈α~no␈α≠exchange␈α≠of␈α≠information␈α≠between␈α~the
␈↓ ↓H␈↓subcomputations.  In a more complicted situation this might also be necessary.
␈↓ ↓H␈↓44␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓8.  ␈↓αLots of LISP functions to program.␈↓


␈↓ ↓H␈↓        The␈α∞following␈α∞are␈α∞descriptions␈α
of␈α∞functions␈α∞on␈α∞lists␈α
and␈α∞S-expressions.␈α∞ You␈α∞should␈α
write
␈↓ ↓H␈↓LISP␈α⊂programs␈α⊂to␈α⊂compute␈α⊂these␈α∂functions.␈α⊂ A␈α⊂description␈α⊂of␈α⊂the␈α∂form␈α⊂ "␈↓↓foo[x]␈↓␈α⊂is␈α⊂true␈α⊂if␈α∂and
␈↓ ↓H␈↓only if ..."␈α means␈αyour␈αprogram␈αshould␈αreturn␈α␈↓¬T␈↓␈αin␈αthe␈αcase␈αthat␈α␈↓↓foo[x]␈↓␈αis␈αtrue␈αand␈α␈↓¬NIL␈↓␈αotherwise.
␈↓ ↓H␈↓Write␈αyour␈αsolutions␈αout␈αin␈αexternal␈αform,␈αif␈αyou␈αlike,␈αthen␈αconvert␈αthem␈αto␈αinternal␈αform␈αand␈αtry
␈↓ ↓H␈↓them␈α
out␈α∞in␈α
whatever␈α∞LISP␈α
system␈α
you␈α∞have␈α
access␈α∞to.␈α
  You␈α
should␈α∞convince␈α
yourself␈α∞by␈α
some
␈↓ ↓H␈↓informal␈αprocess␈αof␈αreasoning␈αthat␈αyour␈αsolutions␈αare␈αindeed␈αcorrect.␈α You␈αwill␈αbe␈αasked␈αto␈αprove
␈↓ ↓H␈↓various␈α∞facts␈α∞about␈α∞your␈α∞programs␈α∞after␈α∞you␈α∞have␈α∞studied␈α∞Chapter␈α∞III.␈α∞ This␈α∞should␈α
encourage
␈↓ ↓H␈↓you␈α
to␈αwrite␈α
clean,␈αunderstandable␈α
programs␈αand␈α
document␈αthem␈α
carefully␈αso␈α
you␈α
will␈αremember
␈↓ ↓H␈↓why you thought they were correct to begin with and what tricks you may have employed.

␈↓ ↓H␈↓        The␈α⊃problems␈α⊃are␈α∩classified␈α⊃according␈α⊃to␈α∩the␈α⊃intended␈α⊃interpretation␈α∩of␈α⊃the␈α⊃lists␈α∩or␈α⊃S-
␈↓ ↓H␈↓expressions.␈α∞ The␈α∞classes␈α∞include␈α∞lists,␈α∞S-expressions,␈α∞arithmetic␈α∞expressions,␈α∞polynomials,␈α∞(finite)
␈↓ ↓H␈↓sets,␈α∞permutations,␈α∞trees,␈α∂and␈α∞graphs.␈α∞ Each␈α∂group␈α∞begins␈α∞with␈α∂some␈α∞simple␈α∞problems␈α∂(with␈α∞one
␈↓ ↓H␈↓line␈α
solutions)␈αto␈α
get␈α
you␈αgoing.␈α
 For␈αthe␈α
more␈α
complicated␈αfunctions,␈α
you␈αwill␈α
find␈α
that␈αdefining
␈↓ ↓H␈↓auxiliary␈α⊃functions␈α⊃is␈α⊃useful.␈α⊃ Towards␈α⊃the␈α∩end␈α⊃the␈α⊃programs␈α⊃may␈α⊃require␈α⊃a␈α⊃fair␈α∩amount␈α⊃of
␈↓ ↓H␈↓thinking␈α∪to␈α∪get␈α∪things␈α∪organized␈α∪correctly,␈α∪but␈α∪none␈α∪of␈α∪the␈α∪problems␈α∪require␈α∪really␈α∩lengthy
␈↓ ↓H␈↓programs.␈α Pay␈αattention␈αto␈αgetting␈αthe␈αtrivial␈αcases␈α(e.␈αg.␈αwhen␈αsome␈αof␈αthe␈αarguments␈αare␈α␈↓¬NIL␈↓␈αor
␈↓ ↓H␈↓atomic␈α
or␈αotherwise␈α
to␈α
be␈αconsidered␈α
basic)␈αcorrect.␈α
 It␈α
is␈αin␈α
general␈αimportant␈α
to␈α
understand␈αthe
␈↓ ↓H␈↓trivial cases in the computation of a function.

␈↓ ↓H␈↓        The␈αobservant␈αreader␈αwill␈αno␈αdoubt␈αnotice␈αthat␈αsome␈αof␈αthese␈αfunctions␈αare␈αdefined␈αin␈αlater
␈↓ ↓H␈↓chapters.␈α∞ Thus␈α∞you␈α∞will␈α∞have␈α
some␈α∞cases␈α∞where␈α∞you␈α∞can␈α
compare␈α∞your␈α∞solution␈α∞to␈α∞those␈α
given.
␈↓ ↓H␈↓(Pointers to these definitions can be found in the function index.)

␈↓ ↓H␈↓α␈↓ ε(Lists

␈↓ ↓H␈↓1. ␈↓↓samelength[u,v]␈↓ tests whether the lists ␈↓↓u␈↓ and ␈↓↓v␈↓ have the same length.

␈↓ ↓H␈↓␈↓ ∧]␈↓↓samelength[␈↓¬(A B C), (D E F)␈↓↓] = ␈↓¬T␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧]␈↓↓samelength[␈↓¬(A B C), (A C)␈↓↓] = ␈↓¬NIL␈↓↓␈↓

␈↓ ↓H␈↓    Do not use any operations or test on numbers to write your program.

␈↓ ↓H␈↓2. ␈↓↓prup[u,v]␈↓ is the list formed by pairing the corresponding elements of the lists ␈↓↓u␈↓ and ␈↓↓v.␈↓  Thus

␈↓ ↓H␈↓␈↓ α`␈↓↓prup[␈↓¬(X Y Z), (1 A (FOO BAR))␈↓↓] = ␈↓¬((X . 1) (Y . A) (Z FOO BAR))␈↓↓␈↓. 

␈↓ ↓H␈↓3.␈α
␈↓↓istail[u,v]␈↓␈α
is␈αtrue␈α
if␈α
and␈α
only␈αif␈α
the␈α
list␈α
␈↓↓u␈↓␈αis␈α
a␈α
tail␈α
of␈α␈↓↓v.␈↓␈α
 That␈α
is␈α
when␈α␈↓↓cdr␈↓ing␈α
through␈α
␈↓↓v␈↓␈αyou␈α
will
␈↓ ↓H␈↓␈↓ ↓xeventually get to ␈↓↓u.␈↓

␈↓ ↓H␈↓4.␈α∞␈↓↓commontail[u,v]␈↓␈α∂is␈α∞the␈α∂longest␈α∞common␈α∂sublist␈α∞of␈α∂␈↓↓u␈↓␈α∞and␈α∂␈↓↓v␈↓␈α∞ending␈α∂with␈α∞the␈α∂ends␈α∞of␈α∂the␈α∞lists.
␈↓ ↓H␈↓␈↓ ↓xThus

␈↓ ↓H␈↓␈↓ ∧β␈↓↓commontail[␈↓¬(A B C D E),(A C D E)␈↓↓] = ␈↓¬(C D E)␈↓↓␈↓. 

␈↓ ↓H␈↓5.␈α∞ ␈↓↓upto[u,v]␈↓␈α∞when␈α
␈↓↓u␈↓␈α∞is␈α∞a␈α
tail␈α∞of␈α∞␈↓↓v,␈↓␈α
is␈α∞the␈α∞list␈α
of␈α∞elements␈α∞of␈α
␈↓↓v␈↓␈α∞upto␈α∞the␈α
point␈α∞where␈α∞␈↓↓u␈↓␈α
begins.
␈↓ ↓H␈↓␈↓ ↓xThus
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *45


␈↓ ↓H␈↓␈↓ ∧L␈↓↓upto[␈↓¬(C D E),(A B C D E)␈↓↓] = ␈↓¬(A B)␈↓↓␈↓. 

␈↓ ↓H␈↓6. ␈↓↓mapapp[f,u]␈↓ appends together the lists obtained by applying ␈↓↓f␈↓ to successive elements of ␈↓↓u.␈↓

␈↓ ↓H␈↓7. ␈↓↓mapchoose[p,u]␈↓ forms a list of those elements of ␈↓↓u␈↓ satisfying ␈↓↓p.␈↓

␈↓ ↓H␈↓8.␈α∪␈↓↓partition[u,n]␈↓␈α∪is␈α∪the␈α∪list␈α∪of␈α∪partitions␈α∪of␈α∪the␈α∩list␈α∪␈↓↓u␈↓␈α∪into␈α∪␈↓↓n␈↓␈α∪sublists␈α∪␈↓↓u1,␈↓␈α∪...␈α∪␈↓↓un␈↓␈α∪such␈α∩that
␈↓ ↓H␈↓␈↓ ↓x␈↓↓u1*[u2*...*un]=u␈↓.␈α If␈α␈↓↓n␈↓␈αis␈αgreater␈αthan␈αthe␈α
length␈αof␈α␈↓↓u␈↓␈αthen␈αyour␈αprogram␈αshould␈α
return␈αan
␈↓ ↓H␈↓␈↓ ↓xerror message of some kind.  Thus

␈↓ ↓H␈↓␈↓ β}␈↓↓partition[␈↓¬(A B C),2␈↓↓]=␈↓¬((A) (B C))((A B) (C)))␈↓↓␈↓ 

␈↓ ↓H␈↓   Write␈α
a␈α
program␈α
␈↓↓testpart␈↓␈α
to␈α
test␈α
the␈αresult␈α
returned␈α
by␈α
␈↓↓partition.␈↓␈α
 For␈α
each␈α
partition,␈α␈↓↓testpart␈↓
␈↓ ↓H␈↓␈↓ ↓xshould append together the lists ␈↓↓ui␈↓ and check that the result is ␈↓↓u.␈↓

␈↓ ↓H␈↓α␈↓ ¬nS-expressions

␈↓ ↓H␈↓9. ␈↓↓mirror[x]␈↓ returns the mirror image of an S-expression ␈↓↓x.␈↓  Thus

␈↓ ↓H␈↓␈↓ ∧;␈↓↓mirror[␈↓¬((A.B).(C.D))␈↓↓]=␈↓¬((D.C).(B.A))␈↓↓␈↓. 

␈↓ ↓H␈↓10. ␈↓↓occur[x,y]␈↓ is true if and only if the atom ␈↓↓x␈↓ occurs in the S-expression ␈↓↓y.␈↓  For example

␈↓ ↓H␈↓␈↓ ¬!␈↓↓occur[␈↓¬B, ␈↓↓␈↓¬((A.B).C)␈↓↓] = ␈↓¬T␈↓↓␈↓. 

␈↓ ↓H␈↓11. ␈↓↓multiplicity[x,y]␈↓ is the number of times the atom ␈↓↓x␈↓ occurs in the S-expression ␈↓↓y.␈↓

␈↓ ↓H␈↓        A␈αpath␈αin␈αan␈αS-expression␈α␈↓↓x␈↓␈αis␈αa␈αlist␈αof␈α␈↓¬A␈↓'s␈αand␈α␈↓¬D␈↓'s␈αsuch␈αthat␈αyou␈αcan␈αapply␈αa␈αsuccession␈αof
␈↓ ↓H␈↓␈↓ ↓x␈↓αa␈↓'s␈αor␈α␈↓αd␈↓'s␈αto␈α␈↓↓x␈↓␈α(according␈αto␈αwhether␈αthe␈αnext␈αlist␈αelement␈αis␈α␈↓¬A␈α␈↓or␈α␈↓¬D␈↓)␈α without␈αtrying␈αto␈αapply␈α␈↓αa␈↓
␈↓ ↓H␈↓␈↓ ↓xor␈α␈↓αd␈↓␈αto␈α
an␈αatom.␈α We␈α
say␈αthat␈αthe␈αresulting␈α
subexpression␈αis␈αat␈α
the␈αend␈αof␈α
the␈αpath␈αor␈αthat␈α
the
␈↓ ↓H␈↓␈↓ ↓xpath␈α
leads␈α
to␈α
that␈α
subexpression.␈α
 Thus␈α␈↓¬(A D)␈↓␈α
is␈α
a␈α
path␈α
in␈α
␈↓¬((X . (Y . Z)) . F)␈↓␈α but␈α
␈↓¬(D A)␈↓
␈↓ ↓H␈↓␈↓ ↓xis not.  Also ␈↓¬(Y . Z)␈↓ is at the end of the path ␈↓¬(A D)␈↓ in ␈↓¬((X . (Y . Z)) . F)␈↓.

␈↓ ↓H␈↓12. ␈↓↓ispath[p,x]␈↓ is true if and only if ␈↓↓p␈↓ is a path in ␈↓↓x.␈↓

␈↓ ↓H␈↓13. ␈↓↓depth[x]␈↓ is the length of the longest path leading to an atom in the S-expression ␈↓↓x.␈↓

␈↓ ↓H␈↓␈↓ ∧m␈↓↓depth[␈↓¬(((A . B) . C) . D␈↓↓] = ␈↓¬3␈↓↓␈↓. 

␈↓ ↓H␈↓        We␈α∞say␈α∂that␈α∞an␈α∂S-expression␈α∞is␈α∂balanced␈α∞if␈α∞it␈α∂is␈α∞an␈α∂atom␈α∞or␈α∂if␈α∞␈↓↓depth[␈↓αa|␈↓↓x]␈↓␈α∂and␈α∞␈↓↓depth[␈↓αd|␈↓↓x]␈↓
␈↓ ↓H␈↓␈↓ ↓xdiffer by at most 1 and ␈↓αa|␈↓␈↓↓x␈↓ and ␈↓αd|␈↓␈↓↓x␈↓ are both balanced.

␈↓ ↓H␈↓14. ␈↓↓balanced[x]␈↓ is true if and only if the S-expression ␈↓↓x␈↓ is balanced.

␈↓ ↓H␈↓15.  ␈↓↓balance␈↓ x is balanced and has the same fringe as x

␈↓ ↓H␈↓16. ␈↓↓get[y,p]␈↓ is the subexpression at the end of the path ␈↓↓p␈↓ in the S-expression ␈↓↓y.␈↓  Thus

␈↓ ↓H␈↓␈↓ ∧>␈↓↓get[␈↓¬(A ((B) C) (B)), (D A A)␈↓↓] = ␈↓¬(B)␈↓↓␈↓. 
␈↓ ↓H␈↓46␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓17.␈α∂␈↓↓point[x,y]␈↓␈α∂is␈α⊂the␈α∂path␈α∂in␈α⊂the␈α∂S-expression␈α∂␈↓↓y␈↓␈α⊂leading␈α∂to␈α∂the␈α⊂left-most␈α∂occurrence␈α∂of␈α⊂the␈α∂S-
␈↓ ↓H␈↓␈↓ ↓xexpression ␈↓↓x␈↓ in the S-expression ␈↓↓y.␈↓ Thus

␈↓ ↓H␈↓␈↓ ∧0␈↓↓point[␈↓¬(B), (A ((B) C) (B))␈↓↓] = ␈↓¬(D A A)␈↓↓␈↓. 

␈↓ ↓H␈↓18. ␈↓↓allpoint[x,y]␈↓ is a list of all paths ␈↓↓p␈↓ such that ␈↓↓get[y,p] = x␈↓.  Thus

␈↓ ↓H␈↓␈↓ βO␈↓↓allpoint[␈↓¬(B), (A ((B) C) (B))␈↓↓] = ␈↓¬((D A A) (D D A))␈↓↓␈↓. 

␈↓ ↓H␈↓19.␈α␈↓↓commons␈αx␈↓␈αis␈α
a␈αlist␈αof␈αthe␈α
subexpressions␈αof␈αthe␈αS-expression␈α␈↓↓x␈↓␈α
that␈αoccur␈αmore␈αthan␈α
once␈αin
␈↓ ↓H␈↓␈↓ ↓x␈↓↓x,␈↓ each followed by a list of the points (paths leading to the points) where it occurs.

␈↓ ↓H␈↓α␈↓ ∧jSymbolic arithmetic and algebra

␈↓ ↓H␈↓20.␈αLet␈αa␈αpolynomial␈αin␈α␈↓↓x␈↓␈αbe␈αrepresented␈αby␈αa␈αlist␈αof␈αits␈αcoefficients␈αin␈αorder␈αof␈αascending␈αpowers
␈↓ ↓H␈↓␈↓ ↓xof ␈↓↓x.␈↓  Thus ␈↓↓x␈↓∧3␈↓↓+x+5␈↓ is represented by ␈↓¬(5 1 0 1)␈↓.

␈↓ ↓H␈↓␈↓ αH        20.1. ␈↓↓sum[u,v]␈↓  is the sum
␈↓ ↓H␈↓␈↓ αH        20.2. ␈↓↓prod[u,v]␈↓  is the product
␈↓ ↓H␈↓␈↓ αH        20.3. ␈↓↓quot[u,v]␈↓ is the quotient
␈↓ ↓H␈↓␈↓ αH        20.4. ␈↓↓rem[u,v]␈↓ is the remainder

␈↓ ↓H␈↓    of the polynomials ␈↓↓u␈↓ and ␈↓↓v␈↓ in the same notation.

␈↓ ↓H␈↓21.  Consider arithmetic expressions as represented in this chapter.  Namely an expression is

␈↓ ↓H␈↓␈↓ αH        (i) a number (satisfies ␈↓↓numberp),␈↓
␈↓ ↓H␈↓␈↓ αH        (ii) a variable (not a number and satisfies ␈↓αat␈↓),
␈↓ ↓H␈↓␈↓ αH        (iii) a sum : ␈↓¬PLUS ␈↓. < list of expressions > or
␈↓ ↓H␈↓␈↓ αH        (iv) a product : ␈↓¬TIMES ␈↓. < list of expressions >.

␈↓ ↓H␈↓    (For simplicity, assume the sum and product lists always have at least 2 elements.)

␈↓ ↓H␈↓        The␈α∂function␈α∂␈↓↓sop␈↓␈α∂converts␈α∂such␈α∂expressions␈α∂into␈α∂sum␈α∂of␈α∂products␈α∂form,␈α∂eg.␈α⊂the␈α∂resulting
␈↓ ↓H␈↓␈↓ ↓xexpression␈α↔is␈α↔either␈α↔a␈α↔monomial␈α↔or␈α↔a␈α↔sum␈α↔of␈α↔monomial␈α↔terms␈α↔which␈α↔has␈α↔the␈α⊗form
␈↓ ↓H␈↓␈↓ ↓x␈↓¬PLUS␈↓ . <list of monomials>.␈α
 A␈α
monomial␈α∞is␈α
either␈α
a␈α∞number,␈α
a␈α
variable,␈α∞or␈α
a␈α
product␈α∞of␈α
the
␈↓ ↓H␈↓␈↓ ↓xform ␈↓¬TIMES␈↓ . < list of numbers or variables >.

␈↓ ↓H␈↓        Try your simplifier on expressions returned by ␈↓↓diff.␈↓

␈↓ ↓H␈↓α␈↓ ε-Sets

␈↓ ↓H␈↓22.␈α∞Using␈α∞the␈α
function␈α∞␈↓↓member[x,␈α∞u]␈↓␈α∞defined␈α
earlier␈α∞in␈α∞this␈α∞chapter,␈α
which␈α∞may␈α∞also␈α∞be␈α
written
␈↓ ↓H␈↓␈↓ ↓x␈↓↓x ε u␈↓,␈α∞write␈α∞function␈α∞definitions␈α∞for␈α
the␈α∞union␈α∞␈↓↓u ∪ v␈↓␈α∞of␈α∞lists␈α
␈↓↓u␈↓␈α∞and␈α∞␈↓↓v,␈↓␈α∞the␈α∞intersection␈α
␈↓↓u ∩ v␈↓,
␈↓ ↓H␈↓␈↓ ↓xand the set difference  ␈↓↓u-v␈↓.   What is wanted should be clear from the examples:

␈↓ ↓H␈↓␈↓ ∧W␈↓↓␈↓¬(A B C) ∪ (B C D) = (A B C D)␈↓↓,␈↓
␈↓ ↓H␈↓␈↓ ∧c␈↓↓␈↓¬(A B C) ∩ (B C D) = (B C)␈↓↓,    ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ∧k␈↓↓␈↓¬(A B C) - (B C D) = (A)␈↓↓.      ␈↓
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *47


␈↓ ↓H␈↓23.␈αSuppose␈α␈↓↓x␈↓␈αtakes␈αnumbers␈αas␈αvalues␈αand␈α␈↓↓u␈↓␈αtakes␈αas␈αvalues␈αlists␈αof␈αnumbers␈αin␈αascending␈αorder,
␈↓ ↓H␈↓␈↓ ↓xe.g.␈α␈↓¬(2␈α4␈α7)␈↓.␈α Write␈αa␈αfunction␈α␈↓↓insert[x,␈αu]␈↓␈αwhose␈αvalue␈αis␈αobtained␈αfrom␈αthat␈αof␈α␈↓↓u␈↓␈αby␈αputting
␈↓ ↓H␈↓␈↓ ↓x␈↓↓x␈↓ in ␈↓↓u␈↓ in its proper place.   Thus

␈↓ ↓H␈↓␈↓ ¬
␈↓↓insert[␈↓¬3, ␈↓↓␈↓¬(2 4)␈↓↓] = ␈↓¬(2 3 4)␈↓↓␈↓,
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ¬
␈↓↓insert[␈↓¬3, ␈↓↓␈↓¬(2 3)␈↓↓] = ␈↓¬(2 3 3)␈↓↓␈↓.

␈↓ ↓H␈↓24.␈αWrite␈αfunctions␈αgiving␈αthe␈αunion,␈αintersection,␈αand␈αset␈αdifference␈αof␈αordered␈αlists;␈αthe␈αresult␈αis
␈↓ ↓H␈↓␈↓ ↓xwanted as an ordered list.

␈↓ ↓H␈↓        Note␈α⊃that␈α⊃computing␈α⊃these␈α⊂functions␈α⊃of␈α⊃unordered␈α⊃lists␈α⊂takes␈α⊃a␈α⊃number␈α⊃of␈α⊂comparisons
␈↓ ↓H␈↓␈↓ ↓xproportional␈αto␈αthe␈αsquare␈αof␈αthe␈αnumber␈α
of␈αelements␈αof␈αa␈αtypical␈αlist,␈αwhile␈αfor␈α
ordered␈αlists,
␈↓ ↓H␈↓␈↓ ↓xthe number of comparisons is proportional to the number of elements.

␈↓ ↓H␈↓25. Using ␈↓↓insert,␈↓ write a function ␈↓↓sort␈↓ that transforms an unordered list into an ordered list.

␈↓ ↓H␈↓26.␈α∂Write␈α⊂a␈α∂function␈α⊂␈↓↓goodsort␈↓␈α∂that␈α⊂sorts␈α∂a␈α⊂list␈α∂using␈α⊂a␈α∂number␈α⊂of␈α∂comparisons␈α⊂proportional␈α∂to
␈↓ ↓H␈↓␈↓ ↓x␈↓↓n log n␈↓, where ␈↓↓n␈↓ is the length of the list to be sorted.

␈↓ ↓H␈↓α␈↓ ¬mPermutations

␈↓ ↓H␈↓27.  ␈↓↓rcycle[u]␈↓  is obtained from the list ␈↓↓u␈↓ by moving the last element to the first position.  Thus

␈↓ ↓H␈↓␈↓ ∧w␈↓↓rcycle[␈↓¬(A B C D)␈↓↓] = ␈↓¬(D A B C)␈↓↓␈↓. 

␈↓ ↓H␈↓28. ␈↓↓lcycle[u]␈↓  is obtained from the list ␈↓↓u␈↓ by moving the first element to the last position.  Thus

␈↓ ↓H␈↓␈↓ ∧x␈↓↓lcycle[␈↓¬(A B C D)␈↓↓] = ␈↓¬(B C D A)␈↓↓␈↓. 

␈↓ ↓H␈↓        We␈α
can␈α
think␈α
of␈α
a␈α
permutation␈α
on␈α
␈↓↓n␈↓␈α
objects␈α
as␈α
a␈α
function␈α
which␈α
maps␈α
a␈α
list␈α
␈↓¬(l1␈α∞l2␈α
...
␈↓ ↓H␈↓¬␈↓ ↓xlN)␈↓␈α
of␈α␈↓↓n␈↓␈α
distinct␈αelements␈α
to␈αa␈α
list␈α
containing␈αthe␈α
same␈αelements␈α
in␈α a␈α
different␈α
order.␈α Thus
␈↓ ↓H␈↓␈↓ ↓x␈↓¬(3 4 1 2)␈↓␈α↔is␈α_a␈α↔permutation␈α↔of␈α_␈↓¬(1 2 3 4)␈↓␈α↔and␈α↔␈↓¬(FOO BAR BAZ)␈↓␈α_is␈α↔a␈α_permutation␈α↔of
␈↓ ↓H␈↓␈↓ ↓x␈↓¬(BAR BAZ FOO)␈↓.␈α∂ The␈α⊂permutation␈α∂itself␈α⊂can␈α∂be␈α⊂represented␈α∂as␈α⊂a␈α∂list␈α⊂in␈α∂several␈α⊂ways.␈α∂ We
␈↓ ↓H␈↓␈↓ ↓xdescribe two possible representations of a permutation ␈↓↓pi.␈↓

␈↓ ↓H␈↓        ␈↓¬REP1:␈α
␈↓␈α␈↓↓pi␈↓␈α
is␈αrepresented␈α
as␈α
a␈αlist␈α
of␈αnumbers␈α
␈↓¬(j1 j2 ... jN)␈↓␈α
which␈αis␈α
itself␈αa␈α
permutation
␈↓ ↓H␈↓␈↓ ↓xof␈α
the␈α
list␈α
␈↓¬(1 2 ... N)␈↓␈α
and␈α
the␈α
result␈α
of␈α
applying␈α
such␈α
a␈α
permutation␈α
to␈α
a␈α
list␈α
␈↓↓u␈↓␈α
of␈α
length␈α
␈↓↓n␈↓␈α
is
␈↓ ↓H␈↓␈↓ ↓xthe␈αlist␈α␈↓↓u'␈↓␈αwhere␈αthe␈α␈↓↓k␈↓th␈αelement␈αof␈α␈↓↓u'␈↓␈αis␈αthe␈α␈↓↓jk␈↓th␈αelement␈αof␈α␈↓↓u.␈↓␈α Thus␈αthe␈αpermutation␈α␈↓¬(2␈α3
␈↓ ↓H␈↓¬␈↓ ↓x4␈α∪1)␈↓␈α∩applied␈α∪to␈α∪the␈α∩list␈α∪␈↓¬(A␈α∪B␈α∩C␈α∪D)␈↓␈α∪is␈α∩the␈α∪list␈α∪␈↓¬(B␈α∩C␈α∪D␈α∪A)␈↓.␈α∩ ␈↓¬(1␈α∪2␈α∪3␈α∩4)␈↓␈α∪is␈α∪the␈α∩identity
␈↓ ↓H␈↓␈↓ ↓xpermutation.

␈↓ ↓H␈↓        ␈↓¬REP2:␈α␈↓␈α
␈↓↓pi␈↓␈αis␈α
represented␈αas␈α
a␈αlist␈α
of␈αdisjoint␈α
cycles,␈αwhere␈α
a␈αcycle␈α
is␈αa␈α
a␈αlist␈α
of␈αlength␈αat␈α
most
␈↓ ↓H␈↓␈↓ ↓x␈↓↓n␈↓␈α
of␈αnumbers␈α
between␈α1␈α
and␈α␈↓↓n␈↓␈α
(with␈αno␈α
two␈αelements␈α
the␈αsame).␈α
 The␈αpermutation␈α
represented
␈↓ ↓H␈↓␈↓ ↓xby␈αsuch␈αa␈αlist␈αof␈αcycles␈αis␈αthe␈αpermutation␈αthat␈αresults␈αfrom␈αapplying␈αeach␈αof␈αthe␈αcycles.␈α (The
␈↓ ↓H␈↓␈↓ ↓xorder␈αof␈αapplication␈αdoesn't␈αmatter␈αsince␈αthe␈α
cycles␈αare␈αdisjoint.)␈αThe␈αresult␈αof␈αapplying␈αa␈α
cycle
␈↓ ↓H␈↓␈↓ ↓x␈↓¬(j1 j2 ... jk)␈↓␈αto␈αa␈αlist␈α␈↓↓u␈↓␈αof␈α
lenght␈α␈↓↓n␈↓␈αis␈αthe␈αlist␈α␈↓↓u'␈↓␈αwhere␈α
the␈αelement␈αin␈αposition␈α␈↓↓jm␈↓␈αin␈α␈↓↓u'␈↓␈α
is
␈↓ ↓H␈↓␈↓ ↓xthe␈α
element␈α
in␈α
position␈α
␈↓↓jm-1␈↓␈α
in␈α
␈↓↓u␈↓␈α
for␈α
␈↓↓1≤m≤k␈↓␈α
(taking␈α
␈↓↓j0␈↓␈α
to␈α
be␈α
␈↓↓jk).␈↓␈α
 Elements␈α
in␈α
positions␈αnot
␈↓ ↓H␈↓␈↓ ↓xmentioned␈αin␈αthe␈αcycle␈αare␈αunchanged.␈α Thus␈αthe␈αcycle␈α ␈↓¬(1 4 2)␈↓␈αapplied␈αto␈α
␈↓¬(A B C D)␈↓␈αgives
␈↓ ↓H␈↓␈↓ ↓x␈↓¬(B D C A)␈↓.  The empty list of cycles is the identity permutation in this representation.
␈↓ ↓H␈↓48␈↓ ¬}Chapter  II␈↓ H


␈↓ ↓H␈↓29.␈α␈↓↓isperm1[pi,n]␈↓␈α(␈↓↓isperm2[pi,n]␈↓)␈α
is␈αtrue␈αjust␈αif␈α
the␈αlist␈α␈↓↓pi␈↓␈αrepresents␈α
a␈αpermutation␈αon␈α
␈↓↓n␈↓␈αobjects
␈↓ ↓H␈↓␈↓ ↓xaccording to ␈↓¬REP1 ␈↓(␈↓¬REP2␈↓).

␈↓ ↓H␈↓30.␈α␈↓↓sameperm2[pi1,pi2]␈↓␈αis␈αtrue␈αif␈αand␈αonly␈αif␈α␈↓↓pi1␈↓␈αand␈α␈↓↓pi2␈↓␈αrepresent␈αthe␈αsame␈αpermutation.␈α (Note
␈↓ ↓H␈↓␈↓ ↓xthat the representation by ␈↓¬REP2 ␈↓is not unique while in ␈↓¬REP1 ␈↓it is.)

␈↓ ↓H␈↓31.␈α∞␈↓↓rho12[pi]␈↓␈α∞maps␈α
a␈α∞list␈α∞␈↓↓pi␈↓␈α
representing␈α∞a␈α∞permutation␈α
according␈α∞to␈α∞␈↓¬REP1␈α
␈↓to␈α∞a␈α∞list␈α
representing
␈↓ ↓H␈↓␈↓ ↓xthe same permutation according to ␈↓¬REP2. ␈↓ ␈↓↓rho21[u]␈↓ is the inverse map.

␈↓ ↓H␈↓32.␈α
␈↓↓compose1[pi1,pi2]␈↓␈αrepresents␈α
the␈α
permutation␈αthat␈α
results␈α
if␈α␈↓↓pi2␈↓␈α
is␈α
applied␈αfollowed␈α
by␈α␈↓↓pi1,␈↓␈α
all
␈↓ ↓H␈↓␈↓ ↓xrepresented according to ␈↓¬REP1. ␈↓ ␈↓↓compose2[u,v]␈↓ does the same but using ␈↓¬REP2. ␈↓

␈↓ ↓H␈↓33.␈α⊗␈↓↓invert1[pi]␈↓␈α∃(␈↓↓invert2[pi]␈↓)␈α⊗is␈α∃the␈α⊗inverse␈α∃to␈α⊗the␈α∃permutation␈α⊗␈↓↓pi,␈↓␈α∃in␈α⊗␈↓¬REP1␈α⊗␈↓(␈↓¬REP2␈↓).␈α∃ Thus
␈↓ ↓H␈↓␈↓ ↓x␈↓↓compose1[invert1[pi],pi]␈↓ is the identitiy permutation.

␈↓ ↓H␈↓        Note␈α∞that␈α∂one␈α∞way␈α∂to␈α∞solve␈α∂the␈α∞above␈α∞problems␈α∂would␈α∞be␈α∂to␈α∞write␈α∂the␈α∞programs␈α∂for␈α∞one
␈↓ ↓H␈↓␈↓ ↓xrepresentation␈α⊂and␈α⊂use␈α⊃the␈α⊂transformations␈α⊂␈↓↓rho12␈↓␈α⊃and␈α⊂␈↓↓rho21␈↓␈α⊂to␈α⊃obtain␈α⊂those␈α⊂for␈α⊃the␈α⊂other
␈↓ ↓H␈↓␈↓ ↓xrepresentation.␈α⊃ However␈α∩part␈α⊃of␈α∩the␈α⊃purpose␈α∩of␈α⊃the␈α⊃exercise␈α∩is␈α⊃to␈α∩see␈α⊃how␈α∩the␈α⊃different
␈↓ ↓H␈↓␈↓ ↓xrepresentations behave and this solution would defeat that purpose.

␈↓ ↓H␈↓α␈↓ ε$Trees

␈↓ ↓H␈↓         Consider␈α
a␈α
tree␈α
structure␈αrepresented␈α
as␈α
a␈α
list␈αin␈α
the␈α
following␈α
way.␈α
 A␈αleaf␈α
is␈α
any␈α
list␈αof␈α
the
␈↓ ↓H␈↓␈↓ ↓xform ␈↓¬(LEAF e)␈↓ where ␈↓¬e ␈↓is any S-expression.  A tree is either a leaf, or a list of trees.

␈↓ ↓H␈↓34.␈α ␈↓↓leaves␈↓␈αt␈αis␈αa␈αlist␈αof␈αall␈αthe␈αleaves␈αin␈αthe␈αtree␈α␈↓↓t,␈↓␈αin␈αthe␈αsame␈αorder␈αas␈αthey␈αappear␈αin␈α the␈αtree.
␈↓ ↓H␈↓␈↓ ↓x(Compare to ␈↓↓fringe␈↓ for S-expressions.)

␈↓ ↓H␈↓35.␈α
 Let␈α
␈↓↓leafval␈↓␈α
be␈α
an␈α
evaluation␈α
function␈αon␈α
leaves.␈α
 (Assume␈α
␈↓↓leafval␈α
returns␈↓␈α
integers.)␈α
␈↓↓bestleaf␈αt␈↓␈α
is
␈↓ ↓H␈↓␈↓ ↓xa␈αleaf␈αof␈α␈↓↓t␈↓␈αhaving␈αa␈αmaximal␈αvalue␈αaccording␈α␈↓↓leafval.␈αThat␈↓␈αis␈αno␈αother␈αleaf␈αof␈α␈↓↓t␈↓␈αhas␈αa␈αbetter
␈↓ ↓H␈↓␈↓ ↓xvalue.

␈↓ ↓H␈↓36.␈α␈↓↓bestleaf1␈αt␈↓␈αis␈αa␈αleaf␈αof␈α␈↓↓t␈↓␈αof␈αmaximal␈αvalue␈αand␈αamong␈αthose␈αof␈αthe␈αsame␈αvalue,␈α␈↓↓bestleaf1␈αt␈↓␈αhas
␈↓ ↓H␈↓␈↓ ↓xleast depth (is closest to the root).

␈↓ ↓H␈↓α␈↓ ε~graphs

␈↓ ↓H␈↓        Let ␈↓↓g␈↓ be a directed graph represented as a list of lists as described earlier in this chapter.

␈↓ ↓H␈↓37. ␈↓↓isnext[u,v,g]␈↓ is true if and only if ␈↓↓g␈↓ contains an edge from ␈↓↓u␈↓ to ␈↓↓v.␈↓

␈↓ ↓H␈↓38. ␈↓↓successors[u,g]␈↓ is the list of vertices, ␈↓↓v,␈↓ in ␈↓↓g␈↓ such that ␈↓↓isnext[u,v,g].␈↓

␈↓ ↓H␈↓39. ␈↓↓predecessors[u,g]␈↓ is the list of vertices ␈↓↓v,␈↓ in ␈↓↓g␈↓ such that ␈↓↓isnext[v,u,g].␈↓

␈↓ ↓H␈↓40.␈α
␈↓↓undir[g]␈↓␈α
is␈α
true␈αif␈α
and␈α
only␈α
if␈α
␈↓↓g␈↓␈αis␈α
undirected.␈α
 That␈α
is,␈αif␈α
for␈α
every␈α
edge␈α
from␈α␈↓↓u␈↓␈α
to␈α
␈↓↓v␈↓␈α
in␈α␈↓↓g␈↓␈α
there
␈↓ ↓H␈↓␈↓ ↓xis also and edge from ␈↓↓v␈↓ to ␈↓↓u.␈↓

␈↓ ↓H␈↓41.␈α ␈↓↓mkundir[g]␈↓␈α
is␈αthe␈α
graph␈α␈↓↓g1␈↓␈αwith␈α
the␈αsame␈α
vertices␈αas␈α
␈↓↓g,␈↓␈αand␈α such␈α
that␈αthere␈α
is␈αan␈αedge␈α
from
␈↓ ↓H␈↓␈↓ ↓x␈↓↓u␈↓ to ␈↓↓v␈↓ in ␈↓↓g1␈↓ if and only if ␈↓↓g␈↓ has either an edge from ␈↓↓v␈↓ to ␈↓↓u␈↓ or and edge from ␈↓↓u␈↓ to ␈↓↓v.␈↓
␈↓ ↓H␈↓␈↓ ¬}Chapter  II␈↓ *49


␈↓ ↓H␈↓42.␈α
 If␈α
␈↓↓g␈↓␈αis␈α
undirected␈α
then␈α
␈↓↓delete_vertex[v,g]␈↓␈α returns␈α
a␈α
graph␈α
␈↓↓g1␈↓␈αwith␈α
vertices␈α
those␈α
of␈α␈↓↓g␈↓␈α
omitting
␈↓ ↓H␈↓␈↓ ↓x␈↓↓v,␈↓ and edges the same as ␈↓↓g␈↓ omitting those connecting ␈↓↓v␈↓ to another vertex.

␈↓ ↓H␈↓43.␈α∞If␈α
␈↓↓g␈↓␈α∞is␈α
undirected␈α∞then␈α
␈↓↓complement[g]␈↓␈α∞ returns␈α
a␈α∞graph␈α
␈↓↓g1␈↓␈α∞with␈α
vertices␈α∞the␈α
same␈α∞as␈α∞␈↓↓g,␈↓␈α
but
␈↓ ↓H␈↓␈↓ ↓xvertices␈α␈↓↓v␈↓␈αand␈α␈↓↓w␈↓␈αare␈αjoined␈αby␈αan␈αedge␈αin␈α␈↓↓g1␈↓␈αif␈αand␈αonly␈αif␈αthey␈αare␈αnot␈αjoined␈αby␈αan␈αedge␈αin
␈↓ ↓H␈↓␈↓ ↓x␈↓↓g.␈↓

␈↓ ↓H␈↓44.␈α
For␈α
any␈α
graph␈α␈↓↓g,␈↓␈α
␈↓↓reachable[u,v,g]␈↓␈α
is␈α
true␈α
if␈αand␈α
only␈α
if␈α
there␈αis␈α
a␈α
sequence␈α
of␈α
vertices␈α␈↓↓u1,␈↓
␈↓ ↓H␈↓␈↓ ↓x␈↓↓u2,␈↓ ... ,␈↓↓un␈↓  in ␈↓↓g␈↓ with ␈↓↓u␈↓ = ␈↓↓u1,␈↓ ␈↓↓v␈↓ = ␈↓↓un␈↓ and ␈↓↓isnext[ui,ui+1,g]␈↓ for ␈↓↓1≤i≤n-1.␈↓

␈↓ ↓H␈↓45.␈αFor␈αany␈αgraph␈α␈↓↓g,␈↓␈α␈↓↓conn[g]␈↓␈αis␈αtrue␈αif␈αand␈αonly␈αif␈αthe␈αdirected␈αgraph␈α␈↓↓g␈↓␈αis␈αconnected␈αin␈αthe␈αsense
␈↓ ↓H␈↓␈↓ ↓xthat every vertex is reachable from every other vertex.

␈↓ ↓H␈↓NB:␈α
 Graphs␈αin␈α
general␈αhave␈α
cycles,␈αso␈α
when␈αyou␈α
are␈αrecurring␈α
through␈αa␈α
graph␈αyou␈α
need␈αto␈α
keep
␈↓ ↓H␈↓␈↓ ↓xtrack of where you have been in order to avoid a forever looping program.
␈↓ ↓H␈↓50␈↓ εH␈↓ H


␈↓ ↓H␈↓α␈↓ ¬zChapter III

␈↓ ↓H␈↓α␈↓ ∧εPROVING FACTS ABOUT LISP PROGRAMS


␈↓ ↓H␈↓        In␈α
this␈α∞chapter␈α
we␈α
begin␈α∞the␈α
"Proving"␈α∞part␈α
of␈α
the␈α∞book.␈α
 In␈α
particular␈α∞we␈α
will␈α∞explain␈α
a
␈↓ ↓H␈↓method␈α∂for␈α∞proving␈α∂␈↓↓extensional␈↓␈α∞properties␈α∂of␈α∞a␈α∂restricted␈α∞but␈α∂powerful␈α∞class␈α∂of␈α∂LISP␈α∞programs
␈↓ ↓H␈↓which␈α
we␈α
call␈α
␈↓↓clean,␈↓␈α
␈↓↓pure␈↓␈α
LISP␈α
programs.␈α
 We␈α
will␈α
give␈α
a␈α
number␈α
of␈α
simple␈α
examples␈α
to␈α
illustrate
␈↓ ↓H␈↓the␈αmethod.␈α  The␈αfollowing␈αchapter␈αis␈αdevoted␈αto␈αa␈αfew␈αmore␈αcomplex␈αexamples.␈α We␈αwill␈αsee␈αin
␈↓ ↓H␈↓later␈αchapters␈αhow␈αthe␈αproof␈αtechniques␈αcan␈αbe␈αextended␈αto␈αcover␈αa␈αlarger␈αclass␈αof␈αproperties␈αand
␈↓ ↓H␈↓a larger class of programs.

␈↓ ↓H␈↓        An␈α␈↓↓extensional␈↓␈α
␈↓↓property␈↓␈αis␈α
one␈αthat␈α
depends␈αonly␈α
on␈αthe␈α
function␈αcomputed␈α
by␈αthe␈α
program.
␈↓ ↓H␈↓That␈αtwo␈α
sort␈αprograms␈αcompute␈α
the␈αsame␈αfunction␈α
and␈αthat␈α␈↓↓append␈↓␈α
is␈αassociative␈αare␈α
extensional
␈↓ ↓H␈↓facts,␈α
but␈αthat␈α
one␈αsort␈α
program␈α
does␈α␈↓↓n␈↓∧2␈↓↓␈↓␈α
comparisons␈αand␈α
another␈α
␈↓↓n log n␈↓␈αcomparisons␈α
or␈αthat␈α
one
␈↓ ↓H␈↓program␈α∞for␈α
␈↓↓reverse␈↓␈α∞does␈α
fewer␈α∞␈↓↓cons␈↓es␈α
than␈α∞another␈α
are␈α∞not.␈α
 These␈α∞latter␈α
facts␈α∞are␈α∞examples␈α
of
␈↓ ↓H␈↓␈↓↓intensional␈↓␈α∩properties.␈α∩ ␈↓↓Clean␈↓␈α∩LISP␈α∩programs␈α∪have␈α∩no␈α∩side␈α∩effects␈α∩(our␈α∩methods␈α∪require␈α∩the
␈↓ ↓H␈↓freedom␈α∪to␈α∪replace␈α∪a␈α∪subexpression␈α∪by␈α∪an␈α∪equal␈α∪expression),␈α∪and␈α∪equality␈α∪refers␈α∪to␈α∪the␈α∩S-
␈↓ ↓H␈↓expressions␈αand␈αnot␈αto␈αthe␈αlist␈αstructures.␈α ␈↓↓Pure␈↓␈αLISP␈αinvolves␈αonly␈αrecursive␈αfunction␈αdefinitions
␈↓ ↓H␈↓and␈α
doesn't␈α
use␈α
assignment␈α
statements.␈α
 So␈α
far␈α
we␈α
have␈α
only␈α
discussed␈α
how␈α
to␈α
write␈α
clean␈α
and␈α
pure
␈↓ ↓H␈↓LISP programs.   The constructs to be explained in Chapter V will take us out of this realm.

␈↓ ↓H␈↓        The␈α∞basic␈α
idea␈α∞is␈α
to␈α∞represent␈α
both␈α∞programs␈α
and␈α∞the␈α
properties␈α∞we␈α
want␈α∞to␈α∞prove␈α
about
␈↓ ↓H␈↓them␈α
as␈α∞sentences␈α
in␈α∞first␈α
order␈α∞logic.␈α
 The␈α∞methods␈α
of␈α∞first-order␈α
logic␈α∞are␈α
well␈α∞developed␈α
and
␈↓ ↓H␈↓understood.␈α∪ Although␈α∪we␈α∪present␈α∪the␈α∪proofs␈α∪somewhat␈α∪informally,␈α∪they␈α∪can␈α∪easily␈α∀be␈α∪fully
␈↓ ↓H␈↓formalized.␈α∃ After␈α∃a␈α∃brief␈α∀introduction␈α∃to␈α∃the␈α∃idea␈α∀of␈α∃formalization,␈α∃we␈α∃give␈α∃an␈α∀informal
␈↓ ↓H␈↓exposition␈α∪of␈α∪first␈α∪order␈α∀logic␈α∪and␈α∪describe␈α∪the␈α∀theories␈α∪of␈α∪S-expressions,␈α∪lists␈α∀and␈α∪natural
␈↓ ↓H␈↓numbers.␈α Next␈αcome␈αtechniques␈αfor␈αproving␈αproperties␈αof␈αprograms␈αknown␈αin␈αadvance␈αto␈αalways
␈↓ ↓H␈↓terminate.␈α∂ After␈α∂this␈α∂come␈α∞the␈α∂more␈α∂difficult␈α∂techniques␈α∞required␈α∂when␈α∂termination␈α∂cannot␈α∞be
␈↓ ↓H␈↓assumed and termination or non-termination must be proved.

␈↓ ↓H␈↓        We␈α
attempt␈α
to␈α
motivate␈α
and␈α
intuitively␈α
justify␈α
the␈α
methods␈α
of␈α
representation␈α
of␈αprograms,
␈↓ ↓H␈↓but␈α∞no␈α∞formal␈α∞proof␈α∞of␈α∞soundness␈α∞is␈α∞given.␈α∞  Much␈α∞of␈α∞the␈α∞material␈α∞in␈α∞this␈α∞chapter␈α∞is␈α∞based␈α
on
␈↓ ↓H␈↓[[Cartwright␈αand␈αMcCarthy␈α1979]].␈α An␈αextensive␈αtreatment␈αof␈αolder␈αprogram␈αproving␈αtechniques
␈↓ ↓H␈↓is given in [[Manna 1974]].



␈↓ ↓H␈↓1.  ␈↓αAbout Formalizing.␈↓


␈↓ ↓H␈↓        The␈αmain␈αreason␈α
for␈αdeveloping␈αformal␈αmethods␈α
is␈αto␈αbe␈αable␈α
to␈αmake␈αideas␈αand␈α
arguments
␈↓ ↓H␈↓clear␈α
and␈α
precise.␈α This␈α
wins␈α
in␈α
several␈αways.␈α
 For␈α
the␈α
logicians␈αthe␈α
development␈α
of␈αformal␈α
systems
␈↓ ↓H␈↓and␈α∀methods␈α∃of␈α∀reasoning␈α∃allowed␈α∀them␈α∃to␈α∀deal␈α∃with␈α∀paradoxes␈α∃which␈α∀arose␈α∃in␈α∀informal
␈↓ ↓H␈↓arguments␈α⊃and␈α⊃also␈α⊃to␈α⊃prove␈α⊃many␈α⊃interesting␈α⊃things␈α⊃about␈α⊃their␈α⊃systems.␈α⊃ For␈α⊃the␈α⊃computer
␈↓ ↓H␈↓scientist␈αformalization␈αprovides␈α
a␈αway␈αof␈αmaking␈α
your␈αnotions␈αand␈α
reasoning␈αprecise␈αenough␈αto␈α
be
␈↓ ↓H␈↓understood␈αand␈αmanipulated␈α
by␈αa␈αcomputer.␈α
 One␈αgoal,␈αof␈α
course,␈αit␈αto␈α
have␈αthe␈αcomputer␈α
do␈αas
␈↓ ↓H␈↓much of the work for you as possible.

␈↓ ↓H␈↓        In␈α∩a␈α∩formal␈α⊃system␈α∩there␈α∩are␈α⊃notions␈α∩of␈α∩syntax,␈α⊃semantics,␈α∩and␈α∩rules␈α∩for␈α⊃manipulating
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *51


␈↓ ↓H␈↓syntactic␈α
entities␈α
in␈α
a␈αsemantics␈α
preserving␈α
manner.␈α
 Thus␈αthere␈α
are␈α
expressions␈α
formed␈αfrom␈α
some
␈↓ ↓H␈↓given␈α∩collection␈α∩of␈α∩symbols␈α⊃in␈α∩a␈α∩prescribed␈α∩manner,␈α∩a␈α⊃means␈α∩of␈α∩assigning␈α∩meaning␈α∩to␈α⊃these
␈↓ ↓H␈↓expressions, and a notion of deduction.

␈↓ ↓H␈↓        For␈αexample,␈αa␈αprogramming␈αsystem␈αis␈αa␈αformal␈αsystem.␈α It␈αhas␈αa␈αlanguage␈αconsisting␈αof␈αthe
␈↓ ↓H␈↓programs, usually specified by some sort of grammar and recognized by a parser.

␈↓ ↓H␈↓        For␈α⊃reasoning␈α∩about␈α⊃programs␈α⊃we␈α∩will␈α⊃use␈α⊃a␈α∩formal␈α⊃system␈α⊃based␈α∩on␈α⊃first␈α∩order␈α⊃logic.
␈↓ ↓H␈↓Understanding␈α⊂properties␈α⊃of␈α⊂a␈α⊃system␈α⊂based␈α⊃on␈α⊂first␈α⊃ order␈α⊂logic␈α⊃can␈α⊂be␈α⊃often␈α⊂be␈α⊃reduced␈α⊂to
␈↓ ↓H␈↓understanding␈α∩properties␈α∩of␈α∩the␈α∩underlying␈α∪logic.␈α∩ This␈α∩underlying␈α∩logic␈α∩is␈α∩powerful␈α∪and␈α∩in
␈↓ ↓H␈↓general well understood.   Such systems are determined by three basic components:

␈↓ ↓H␈↓␈↓ α_1. a domain with some designated subdomains (called sorts),
␈↓ ↓H␈↓␈↓ α_2. a language whose function and predicate symbols are interpreted in the domain and
␈↓ ↓H␈↓␈↓ α_3. a collection of facts that are true of the domain.

␈↓ ↓H␈↓        Given␈α∪the␈α∪basic␈α∪ components,␈α∪the␈α∀expressions␈α∪of␈α∪the␈α∪language,␈α∪interpretation␈α∀of␈α∪these
␈↓ ↓H␈↓expressions␈α
in␈α
the␈α
domain␈αand␈α
rules␈α
for␈α
(syntactically)␈α
deducing␈αadditional␈α
facts␈α
are␈α
fixed␈α
as␈αwe
␈↓ ↓H␈↓will explain below.

␈↓ ↓H␈↓        Such␈α
a␈αsystem␈α
can␈αbe␈α
mechanized␈αin␈α
the␈αfollowing␈α
sense.␈α We␈α
can␈αwrite␈α
a␈αprogram␈α
that␈αis
␈↓ ↓H␈↓capable␈α⊃of␈α⊃understanding␈α⊃a␈α⊃description␈α⊃of␈α⊃it,␈α⊃build␈α⊃a␈α⊃data␈α⊃structure␈α⊃representing␈α⊃the␈α⊂various
␈↓ ↓H␈↓components,␈α∩check␈α⊃that␈α∩statements␈α⊃are␈α∩well-formed␈α⊃expressions␈α∩of␈α⊃the␈α∩language,␈α⊃and␈α∩that␈α⊃an
␈↓ ↓H␈↓alleged␈αproof␈α
is␈αindeed␈α
a␈αvalid␈α
deduction␈αfrom␈αthe␈α
known␈αfacts.␈α
 Given␈αsuch␈α
a␈αprogram,␈α
we␈αcan
␈↓ ↓H␈↓describe␈αto␈αit␈αour␈αsystem␈αfor␈αproving␈αfacts␈αabout␈αLISP␈αprograms␈αand␈αit␈αwill␈αbe␈αable␈αto␈αcheck␈αthat
␈↓ ↓H␈↓our␈αproofs␈αare␈αcorrect.␈α Such␈αa␈αprogram␈αexists␈αat␈αStanford␈α[[Weyhrauch␈α1977]].␈α Other␈αprograms
␈↓ ↓H␈↓exist␈α∂which␈α∂are␈α∂designed␈α⊂to␈α∂automatically␈α∂prove␈α∂facts␈α⊂about␈α∂LISP␈α∂programs.␈α∂ For␈α⊂example␈α∂see
␈↓ ↓H␈↓[[Boyer and Moore 1978]].



␈↓ ↓H␈↓2.  ␈↓αThe theory of S-expressions.␈↓


␈↓ ↓H␈↓        We␈α∞begin␈α
by␈α∞simply␈α
presenting␈α∞a␈α
formal␈α∞theory,␈α
namely␈α∞the␈α
theory␈α∞of␈α∞S-expressions.␈α
 We
␈↓ ↓H␈↓present␈α∩it␈α∩as␈α∩formalization␈α∪ and␈α∩abstraction␈α∩of␈α∩the␈α∪the␈α∩discussion␈α∩of␈α∩S-expressions␈α∪given␈α∩in
␈↓ ↓H␈↓Chapter␈α∂I.␈α∞ The␈α∂universe␈α∂or␈α∞domain␈α∂in␈α∞question␈α∂contains␈α∂S-expressions␈α∞and␈α∂possibly␈α∂other␈α∞yet
␈↓ ↓H␈↓unspecified␈α⊂objects.␈α∂ The␈α⊂goal␈α∂is␈α⊂to␈α⊂formalize␈α∂properties␈α⊂of␈α∂and␈α⊂syntactically␈α⊂characterize␈α∂some
␈↓ ↓H␈↓sub-domains␈α∂such␈α∂as␈α∞S-expressions,␈α∂lists,␈α∂and␈α∞natural␈α∂numbers␈α∂(viewed␈α∞as␈α∂a␈α∂subdomain␈α∂of␈α∞the
␈↓ ↓H␈↓atomic S-expressions).

␈↓ ↓H␈↓        We␈α∪first␈α∪describe␈α∪the␈α∪language,␈α∪some␈α∪simple␈α∪"boolean"␈α∪facts␈α∪about␈α∪the␈α∪domain␈α∪of␈α∪S-
␈↓ ↓H␈↓expressions␈α∀and␈α∀give␈α∀axioms␈α∀describing␈α∀the␈α∪algebraic␈α∀properties␈α∀of␈α∀basic␈α∀operations␈α∀on␈α∪S-
␈↓ ↓H␈↓expressions.␈α∪ We␈α∩then␈α∪explain␈α∩a␈α∪principle␈α∩of␈α∪induction␈α∩on␈α∪S-expressions␈α∩and␈α∪represent␈α∩this
␈↓ ↓H␈↓principle␈αby␈αthe␈α
schema␈αof␈αS-expression␈α
induction.␈α We␈αdeduce␈α
some␈αadditional␈αproperties␈α
of␈αS-
␈↓ ↓H␈↓expressions as examples of (semi-)formal proofs based on the formal description.

␈↓ ↓H␈↓Our␈α∞precise␈α
notion␈α∞of␈α∞formal␈α
first␈α∞order␈α∞theory␈α
will␈α∞be␈α
explained␈α∞in␈α∞the␈α
next␈α∞section␈α∞before␈α
we
␈↓ ↓H␈↓complete our description of the formal system for reasoning about LISP programs.
␈↓ ↓H␈↓52␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓α␈↓ ∧~The elementary theory of ␈↓↓car,␈↓α ␈↓↓cdr,␈↓α and ␈↓↓cons␈↓α.

␈↓ ↓H␈↓        The␈α
domain␈α∞of␈α
S-expressions␈α
we␈α∞will␈α
denote␈α∞by␈α
␈↓↓SEXP␈↓␈α
and␈α∞the␈α
subdomains␈α∞consisting␈α
of
␈↓ ↓H␈↓the␈αatoms␈αand␈αthe␈αnon-atomic␈αS-expressions␈αwe␈αdenote␈αby␈α␈↓↓ATOM␈↓␈αand␈α␈↓↓PAIR,␈↓␈αrespectively.␈α The
␈↓ ↓H␈↓fact␈α
that␈α
S-expressions␈α
are␈α
the␈α
disjoint␈α∞union␈α
of␈α
atoms␈α
and␈α
non-atoms␈α
is␈α
expressed␈α∞formally␈α
by
␈↓ ↓H␈↓the ␈↓↓domain relations␈↓:

␈↓ ↓H␈↓␈↓ βx␈↓↓SEXP = ATOM ∪ PAIR␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ATOM ∩ PAIR = ␈↓πf␈↓↓␈↓

␈↓ ↓H␈↓        As␈αin␈αthe␈α
external␈αnotation␈αfor␈αLISP␈α
programs,␈αwe␈αwill␈αuse␈α
list␈αand/or␈αdot␈αnotation␈α
for␈αS-
␈↓ ↓H␈↓expression␈αconstants.␈α We␈αmention␈α
two␈αparticular␈αatoms␈α␈↓¬T␈↓␈αand␈α
␈↓¬NIL␈↓.␈α Formally␈αwe␈αspecify␈αthat␈α
they
␈↓ ↓H␈↓are of sort ␈↓↓ATOM␈↓ by the ␈↓↓domain membership facts␈↓:

␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓¬T␈↓↓ ε ATOM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓¬NIL␈↓↓ ε ATOM␈↓

␈↓ ↓H␈↓        In␈α∞order␈α∞to␈α∞make␈α
general␈α∞statements␈α∞about␈α∞S-expressions␈α
we␈α∞need␈α∞variables.␈α∞ We␈α∞will␈α
use
␈↓ ↓H␈↓␈↓↓X,␈↓␈α␈↓↓Y,␈↓␈α␈↓↓Z␈↓␈αas␈αgeneral␈αvariables.␈α They␈αstand␈αfor␈αabitrary␈αelements␈αof␈αthe␈αentire␈αdomain.␈αThere␈αwill
␈↓ ↓H␈↓also␈αbe␈αvariables␈αfor␈αeach␈αof␈αthe␈αsub-domains.␈α ␈↓↓x,␈αy,␈αz␈↓␈αwill␈αstand␈αfor␈α(range␈αover)␈αS-expressions,
␈↓ ↓H␈↓␈↓↓xx,␈α∞yy,␈α∞zz␈↓␈α∞will␈α∂stand␈α∞for␈α∞non-atomic␈α∞S-expressions␈α∂and␈α∞␈↓↓a,␈α∞b,␈α∞c␈↓␈α∂will␈α∞stand␈α∞for␈α∞atoms.␈α∂ This␈α∞is
␈↓ ↓H␈↓expressed formally by the additional domain membership facts:

␈↓ ↓H␈↓␈↓ βx␈↓↓x, y, z ε SEXP␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓xx, yy, zz ε PAIR␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓a, b, c ε ATOM␈↓

␈↓ ↓H␈↓        The␈α
basic␈α
functions␈α
on␈α
S-expressions␈α
are␈α
␈↓↓car,␈↓␈α∞␈↓↓cdr␈↓␈α
and␈α
␈↓↓cons␈↓␈α
which␈α
we␈α
will␈α
denote␈α
by␈α∞␈↓αa␈↓,␈α
␈↓αd␈↓
␈↓ ↓H␈↓and␈α⊂infix␈α⊂" . "␈α⊂(or␈α⊂occasionally␈α⊂by␈α⊂␈↓αcons␈↓)␈α⊂as␈α⊂in␈α⊂the␈α⊂external␈α⊂notation␈α⊂for␈α⊂programs.␈α⊂ That␈α⊂␈↓↓cons␈↓
␈↓ ↓H␈↓applied␈αto␈α
any␈αtwo␈α
S-expressions␈αis␈αa␈α
non-atomic␈αS-expression␈α
and␈α␈↓↓car␈↓␈αand␈α
␈↓↓cdr␈↓␈αapplied␈α
to␈αnon-
␈↓ ↓H␈↓atomic S-expressions produce S-expressions is expressed formally by the ␈↓↓domain mappings␈↓:

␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αa␈↓↓: PAIR → SEXP␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αd␈↓↓: PAIR → SEXP␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αcons␈↓↓: SEXP ␈↓¬x␈↓↓ SEXP → PAIR␈↓

␈↓ ↓H␈↓        We␈α⊃will␈α⊃treat␈α⊃␈↓↓SEXP,␈↓␈α⊃␈↓↓PAIR␈↓␈α⊃and␈α⊃␈↓↓ATOM␈↓␈α⊃as␈α⊃unary␈α⊃predicates␈α⊃on␈α⊃the␈α⊃domain␈α∩with␈α⊃the
␈↓ ↓H␈↓obvious meaning.

␈↓ ↓H␈↓        Now␈αsome␈αfacts␈αabout␈α␈↓↓car,␈↓␈α␈↓↓cdr␈↓␈αand␈α␈↓↓cons.␈↓␈α Recall␈αthat␈αwhen␈αan␈αS-expression␈αis␈αconstructed
␈↓ ↓H␈↓from␈αtwo␈α
given␈αS-expressions,␈αthe␈α
result␈αhas␈αthe␈α
first␈αS-expression␈αin␈α
the␈αa-part␈αand␈α
the␈αsecond
␈↓ ↓H␈↓in the d-part.  This is stated formally by the axioms:

␈↓ ↓H␈↓␈↓ βx␈↓¬CAR:␈↓  ␈↓↓∀x y.␈↓αa|␈↓↓[x . y] = x␈↓
␈↓ ↓H␈↓␈↓ βx␈↓¬CDR:␈↓  ␈↓↓∀x y.␈↓αd|␈↓↓[x . y] = y␈↓

␈↓ ↓H␈↓Since␈α
␈↓↓x,␈↓␈α
and␈α
␈↓↓y␈↓␈α
are␈α∞specified␈α
to␈α
range␈α
over␈α
S-expression,␈α
the␈α∞quantifier␈α
␈↓↓∀x␈α
y.␈↓␈α
means␈α
"for␈α∞all␈α
S-
␈↓ ↓H␈↓expressions ␈↓↓x␈↓ and for all S-expressions ␈↓↓y␈↓".

␈↓ ↓H␈↓        Also␈αgiven␈αa␈αnon-atomic␈αS-expression␈α␈↓↓xx,␈↓␈αthe␈αS-expression␈αconstructed␈αfrom␈α␈↓↓␈↓αa|␈↓↓xx␈↓␈αand␈α␈↓↓␈↓αd|␈↓↓xx␈↓
␈↓ ↓H␈↓is ␈↓αequal␈↓ to ␈↓↓xx␈↓ (although probably not ␈↓αeq␈↓ to ␈↓↓xx␈↓).  Thus
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *53


␈↓ ↓H␈↓␈↓ βx␈↓¬CONS:␈↓  ␈↓↓∀xx.[␈↓αa|␈↓↓xx . ␈↓αd|␈↓↓xx] = xx␈↓

␈↓ ↓H␈↓Since␈α
␈↓↓xx␈↓␈α
is␈α
specified␈αto␈α
range␈α
over␈α
non-atomic␈α
S-expressions,␈αthe␈α
quantifier␈α
␈↓↓∀xx.␈↓␈α
means␈α
"for␈αall
␈↓ ↓H␈↓non-atomic␈αS-expressions␈α␈↓↓xx␈↓"␈α
or␈α"for␈αall␈αpairs␈α
␈↓↓xx␈↓".␈α The␈αaxiom␈α␈↓¬CONS␈α
␈↓also␈αexpresses␈αthe␈α
fact␈αthat
␈↓ ↓H␈↓two␈α∞S-expressions␈α∞are␈α∞equal␈α
if␈α∞and␈α∞only␈α∞if␈α
their␈α∞a-parts␈α∞are␈α∞equal␈α
and␈α∞their␈α∞d-parts␈α∞are␈α
equal.
␈↓ ↓H␈↓That is:

␈↓ ↓H␈↓␈↓ βx␈↓¬EQPAIR:␈↓  ␈↓↓∀xx yy.[[␈↓αa|␈↓↓xx = ␈↓αa|␈↓↓yy] ∧ [␈↓αd|␈↓↓xx = ␈↓αd|␈↓↓yy] ≡ xx = yy]␈↓

␈↓ ↓H␈↓        We␈α∞can␈α∞even␈α∂give␈α∞a␈α∞formal␈α∂proof∨B∞that␈α∞␈↓¬EQPAIR␈α∞␈↓is␈α∂is␈α∞true␈α∞in␈α∂the␈α∞theory␈α∞developed␈α∂so␈α∞far.
␈↓ ↓H␈↓To␈α⊃prove␈α⊂that␈α⊃a␈α⊂statement␈α⊃holds␈α⊂for␈α⊃all␈α⊂␈↓↓xx,␈α⊃yy␈↓␈α⊂it␈α⊃is␈α⊂enough␈α⊃to␈α⊂prove␈α⊃that␈α⊃the␈α⊂unquantified
␈↓ ↓H␈↓statement holds without making any special assumptions about ␈↓↓xx␈↓ or ␈↓↓yy.␈↓  Thus we want to prove

␈↓ ↓H␈↓␈↓ ∧←␈↓↓[␈↓αa|␈↓↓xx = ␈↓αa|␈↓↓yy] ∧ [␈↓αd|␈↓↓xx = ␈↓αd|␈↓↓yy] ≡ xx = yy␈↓ 

␈↓ ↓H␈↓To␈αprove␈αthat␈α
a␈αformula␈αof␈αthe␈α
form␈α␈↓↓A␈α≡␈α
B␈↓␈αis␈αtrue␈αit␈α
is␈αsufficient␈αto␈α
prove␈αthat␈α␈↓↓A␈α⊃␈α
 B␈↓␈αand␈α␈↓↓B␈α⊃␈α
A␈↓
␈↓ ↓H␈↓are␈α∃both␈α∃true␈α∃since␈α∃equivalence␈α∃is␈α∃really␈α∀ an␈α∃abbreviation␈α∃of␈α∃the␈α∃conjunction␈α∃of␈α∃the␈α∀two
␈↓ ↓H␈↓implications.  So we must prove

␈↓ ↓H␈↓␈↓ ∧↑␈↓↓[␈↓αa|␈↓↓xx = ␈↓αa|␈↓↓yy] ∧ [␈↓αd|␈↓↓xx = ␈↓αd|␈↓↓yy] ⊃ xx = yy␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ ∧Y␈↓↓xx = yy ⊃ [␈↓αa|␈↓↓xx = ␈↓αa|␈↓↓yy] ∧ [␈↓αd|␈↓↓xx = ␈↓αd|␈↓↓yy] ␈↓

␈↓ ↓H␈↓To prove ␈↓↓A ⊃ B␈↓ we assume ␈↓↓A␈↓ and show that ␈↓↓B␈↓ follows.  For the first implication, assume

␈↓ ↓H␈↓␈↓ ¬_␈↓↓[␈↓αa|␈↓↓xx = ␈↓αa|␈↓↓yy] ∧ [␈↓αd|␈↓↓xx = ␈↓αd|␈↓↓yy].␈↓ 

␈↓ ↓H␈↓Then by substitition of equals for equals we have

␈↓ ↓H␈↓␈↓ ¬"␈↓↓[␈↓αa|␈↓↓xx . ␈↓αd|␈↓↓xx] = [␈↓αa|␈↓↓yy . ␈↓αd|␈↓↓yy]␈↓ 

␈↓ ↓H␈↓and using ␈↓¬CONS ␈↓twice we obtain the desired conclusion

␈↓ ↓H␈↓␈↓ ε≥␈↓↓xx=yy.␈↓ 

␈↓ ↓H␈↓For the second implication, assume

␈↓ ↓H␈↓␈↓ ε≥␈↓↓xx=yy.␈↓ 

␈↓ ↓H␈↓Then

␈↓ ↓H␈↓␈↓ ¬≡␈↓↓[␈↓αa|␈↓↓xx = ␈↓αa|␈↓↓yy] ∧ [␈↓αd|␈↓↓xx = ␈↓αd|␈↓↓yy]␈↓ 

␈↓ ↓H␈↓follows by subsititution.  This completes the proof.

␈↓ ↓H␈↓        The␈α∂above␈α∞proof␈α∂was␈α∞carried␈α∂out␈α∞in␈α∂a␈α∞"natural␈α∂deduction"␈α∞style␈α∂and␈α∞could␈α∂be␈α∞completely
␈↓ ↓H␈↓formalized␈α
(for␈α
example␈α
so␈α
as␈α
to␈α
be␈α
accepted␈α
by␈α
a␈α
proof␈α
checker␈α
for␈α
natural␈α
deduction␈αstyle␈α
proofs)
␈↓ ↓H␈↓without much additional detail.

␈↓ ↓H␈↓[Remark:␈α⊂A␈α⊂first␈α⊃order␈α⊂theory␈α⊂deduced␈α⊂by␈α⊃formal␈α⊂proof␈α⊂from␈α⊂given␈α⊃axioms␈α⊂will␈α⊂hold␈α⊃in␈α⊂any
␈↓ ↓H␈↓54␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓domain␈α∩where␈α∩the␈α⊃given␈α∩axioms␈α∩hold.␈α⊃ Thus␈α∩we␈α∩are␈α⊃really␈α∩proving␈α∩things␈α⊃about␈α∩a␈α∩class␈α⊃of
␈↓ ↓H␈↓domains, not just one.  ]

␈↓ ↓H␈↓α␈↓ ∧6The principle of S-expression induction.

␈↓ ↓H␈↓        With␈αthe␈α
axioms␈αand␈α
domain␈αfacts␈α
given␈αso␈αfar␈α
we␈αcan␈α
use␈αthe␈α
rules␈αof␈α
logic␈αto␈αprove␈α
many
␈↓ ↓H␈↓addiditonal␈αfacts␈αabout␈αparticular␈αS-expressions␈αand␈αa␈αfew␈αgeneral␈αfacts,␈αbut␈αthere␈αare␈αmany␈αfacts
␈↓ ↓H␈↓about␈α∂S-expressions␈α∞that␈α∂can␈α∂not␈α∞be␈α∂proved.␈α∂ For␈α∞example␈α∂in␈α∂the␈α∞domain␈α∂of␈α∂S-expressions␈α∞we
␈↓ ↓H␈↓have

␈↓ ↓H␈↓␈↓¬NO-RPLACS: ␈↓   ␈↓↓∀x y: x≠[x . y]␈↓

␈↓ ↓H␈↓but␈αthis␈αis␈α
not␈αprovable␈αin␈α
the␈αtheory␈αdeveloped␈αso␈α
far.␈α Indeed␈αthere␈α
are␈αdomains␈αwhere␈αthe␈α
facts
␈↓ ↓H␈↓given␈α∞so␈α
far␈α∞are␈α
true,␈α∞but␈α
␈↓↓∃x y: x=[x . y]␈↓␈α∞is␈α
also␈α∞true.␈α
 (The␈α∞significance␈α
of␈α∞the␈α∞label␈α
␈↓¬NO-RPLACS
␈↓ ↓H␈↓¬␈↓will␈α∞become␈α∞clear␈α∞in␈α∞Chapter␈α∞V␈α∞where␈α
we␈α∞discuss␈α∞operations␈α∞that␈α∞create␈α∞circular␈α∞list␈α
structures.)
␈↓ ↓H␈↓That␈α⊃says␈α∩that␈α⊃the␈α⊃algebraic␈α∩and␈α⊃domain␈α⊃facts␈α∩are␈α⊃insufficient␈α⊃to␈α∩characterize␈α⊃S-expressions.
␈↓ ↓H␈↓Indeed␈α∂Goedel's␈α⊂famous␈α∂theorem␈α⊂extends␈α∂to␈α⊂tell␈α∂us␈α∂that␈α⊂no␈α∂theory␈α⊂in␈α∂which␈α⊂all␈α∂proofs␈α⊂can␈α∂be
␈↓ ↓H␈↓checked␈α∀admits␈α∃proofs␈α∀of␈α∀all␈α∃true␈α∀statements␈α∀about␈α∃S-expressions.␈α∀ However,␈α∃all␈α∀practically
␈↓ ↓H␈↓interesting␈α→sentences␈α~involving␈α→pure␈α~LISP␈α→programs␈α~are␈α→provable␈α~in␈α→the␈α~above␈α→theory
␈↓ ↓H␈↓supplemented by a suitable principle of mathematical induction.

␈↓ ↓H␈↓        Recall␈αthe␈α"inductive"␈αdefinition␈αof␈αS-expression:␈αan␈αS-expression␈αis␈αeither␈αan␈αatom␈αor␈αit␈αis
␈↓ ↓H␈↓the␈α
result␈α
of␈α
applying␈α␈↓↓cons␈↓␈α
to␈α
the␈α
␈↓↓car␈↓␈αand␈α
the␈α
␈↓↓cdr.␈↓␈α
 Further␈α
more␈αeach␈α
S-expression␈α
is␈α
the␈αresult␈α
of
␈↓ ↓H␈↓a␈αfinite␈αnumber␈αof␈α␈↓↓cons␈↓es␈αbegining␈αwith␈αa␈αfinite␈αnumber␈αof␈αatoms.␈α Thus␈αif␈αsome␈αproperty␈αis␈αtrue
␈↓ ↓H␈↓of␈αall␈α
atoms␈αand␈α
whenever␈αit␈αis␈α
true␈αof␈α
␈↓↓x␈↓␈αand␈α
␈↓↓y␈↓␈αit␈αis␈α
true␈αof␈α
␈↓↓[x . y]␈↓␈αthen␈αit␈α
will␈αbe␈α
true␈αof␈α
all␈αS-
␈↓ ↓H␈↓expressions.  This is expressed formally by the S-expression induction schema:

␈↓ ↓H␈↓␈↓¬SEXPINDUCTION: ␈↓␈↓ β}␈↓↓∀a: ␈↓πF␈↓↓ a ∧ ∀xx: [␈↓πF␈↓↓ ␈↓αa|␈↓↓xx ∧ ␈↓πF␈↓↓ ␈↓αd|␈↓↓xx ⊃ ␈↓πF␈↓↓ xx] ⊃ ∀x: ␈↓πF␈↓↓ x ␈↓. 

␈↓ ↓H␈↓Here␈α␈↓πF␈↓␈α
is␈αa␈α
predicate␈αparameter.␈α
 The␈αschema␈α
is␈αused␈α
by␈αsubsituting␈α
a␈αunary␈αpredicate␈α
expression
␈↓ ↓H␈↓(a␈α
formula␈αwith␈α
a␈αdesignated␈α
variable␈αwhich␈α
is␈αto␈α
be␈α
replaced␈αby␈α
the␈αargument␈α
to␈α␈↓πF␈↓)␈α
for␈α␈↓πF␈↓.␈α
 Thus
␈↓ ↓H␈↓the␈αschema␈αstands␈αfor␈αan␈αinfinite␈αcollection␈αof␈αaxioms,␈αone␈αfor␈αeach␈αformula␈αand␈αdesignated␈α(free)
␈↓ ↓H␈↓variable that formula.

␈↓ ↓H␈↓        Using␈α∂S-expression␈α∂induction␈α∂we␈α∂can␈α∞prove␈α∂␈↓¬NO-RPLACS:␈α∂␈↓   ␈↓↓∀x␈α∂y:␈α∂x≠[x . y].␈↓␈α∞Instantiating
␈↓ ↓H␈↓␈↓¬SEXPINDUCTION ␈↓with ␈↓↓␈↓πF␈↓↓ x ≡∀y: x≠[x . y]␈↓ we obtain the particular axiom

␈↓ ↓H␈↓␈↓ ↓J␈↓↓∀a y: a ≠[a . y] ∧ ∀xx: [∀y: ␈↓αa|␈↓↓xx≠[␈↓αa|␈↓↓xx . y]∧∀y: ␈↓αd|␈↓↓xx≠[␈↓αd|␈↓↓xx . y] ⊃ ∀y: xx≠[xx . y]] ⊃ ∀x y: x≠[x . y].␈↓ 

␈↓ ↓H␈↓Thus␈α∂to␈α∂prove␈α∞␈↓↓∀x␈α∂y:x≠[x . y]␈↓␈α∂ it␈α∞is␈α∂sufficient␈α∂to␈α∞prove␈α∂the␈α∂two␈α∞clauses␈α∂in␈α∂the␈α∂hypothesis.␈α∞ The
␈↓ ↓H␈↓clause␈αfor␈αthe␈αATOM␈αcase␈αis␈α␈↓↓∀a y: a␈α≠[a . y].␈↓␈αWe␈αprove␈αit␈αby␈αcontradiction.␈α  That␈αis␈αwe␈αassume
␈↓ ↓H␈↓the␈αnegation,␈α␈↓↓a=[a . y]␈↓,␈α
holds␈αfor␈αsome␈α
atom␈α␈↓↓a␈↓␈αand␈α
S-expression␈α␈↓↓y.␈↓␈α Then␈α
we␈αhave␈αby␈αthe␈α
domain
␈↓ ↓H␈↓equations, function mappings and the fact that ␈↓↓a␈↓ ranges over atoms the following:

␈↓ ↓H␈↓␈↓ ∧␈↓↓ATOM a ∧ PAIR [a . y] ∧ ATOM ∩ PAIR = ␈↓πf␈↓↓.␈↓ 

␈↓ ↓H␈↓Clearly a contradiction.

␈↓ ↓H␈↓The clause for the non-ATOM case is

␈↓ ↓H␈↓␈↓ β2␈↓↓∀xx: [∀y: ␈↓αa|␈↓↓xx≠[␈↓αa|␈↓↓xx . y]∧∀y: ␈↓αd|␈↓↓xx≠[␈↓αd|␈↓↓xx . y] ⊃ ∀y: xx≠[xx . y]].␈↓ 
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *55


␈↓ ↓H␈↓It␈α
is␈α∞proved␈α
by␈α
assuming␈α∞␈↓↓∀y:␈α
␈↓αa|␈↓↓xx≠[␈↓αa|␈↓↓xx . y]␈α
∧␈α∞∀y:␈α
␈↓αd|␈↓↓xx≠[␈↓αd|␈↓↓xx . y]␈α
␈↓␈α∞(the␈α
induction␈α∞hypothesis)␈α
an
␈↓ ↓H␈↓deducing␈α∪␈↓↓∀y: xx≠[xx . y]␈↓.␈α∀ The␈α∪latter␈α∪is␈α∀again␈α∪done␈α∪by␈α∀contradiction.␈α∪ Assume␈α∪for␈α∀some␈α∪S-
␈↓ ↓H␈↓expression␈α⊗␈↓↓y␈↓␈α⊗that␈α↔␈↓↓xx=[xx . y]␈↓.␈α⊗ Then␈α⊗by␈α⊗substitution␈α↔and␈α⊗␈↓¬CAR␈α⊗␈↓we␈α↔have␈α⊗␈↓↓␈↓αa|␈↓↓xx=␈↓αa|␈↓↓[xx . y]=xx␈↓.
␈↓ ↓H␈↓Substituting␈α∞again␈α∂we␈α∞get␈α∂␈↓↓␈↓αa|␈↓↓xx=[␈↓αa|␈↓↓xx . y]␈↓␈α∞which␈α∞contradicts␈α∂the␈α∞induction␈α∂hypothesis,␈α∞completing
␈↓ ↓H␈↓the proof.

␈↓ ↓H␈↓        S-expression␈α↔induction␈α⊗is␈α↔on␈α⊗example␈α↔of␈α⊗a␈α↔class␈α⊗of␈α↔induction␈α⊗principles␈α↔known␈α⊗as
␈↓ ↓H␈↓"structural"␈α⊃induction.␈α∩  Such␈α⊃principles␈α⊃are␈α∩based␈α⊃directly␈α⊃on␈α∩the␈α⊃inductive␈α⊃definition␈α∩of␈α⊃the
␈↓ ↓H␈↓domain␈αto␈αwhich␈αthey␈αapply.␈α We␈αwill␈αlater␈αadd␈αprinciples␈αof␈αlist␈αand␈αnumcerical␈αinduction␈αwhich
␈↓ ↓H␈↓are also structural induction principles.

␈↓ ↓H␈↓        S-expression␈α
induction␈α
could␈αbe␈α
introduced␈α
as␈αa␈α
proof␈α
rule␈αrather␈α
than␈α
an␈α
axiom␈αschema.
␈↓ ↓H␈↓To␈α⊃prove␈α⊃that␈α⊂some␈α⊃formula␈α⊃␈↓↓␈↓πF␈↓↓ x␈↓␈α⊂holds␈α⊃for␈α⊃all␈α⊂S-expressions,␈α⊃␈↓↓x␈↓␈α⊃ (i.e.␈α⊂to␈α⊃prove␈α⊃␈↓↓∀x: ␈↓πF␈↓↓ x␈↓)␈α⊃ it␈α⊂is
␈↓ ↓H␈↓sufficient to

␈↓ ↓H␈↓    (i) prove the ATOM case: ␈↓↓∀a: ␈↓πF␈↓↓ a␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓    (ii) assume the induction hypotheses: ␈↓↓␈↓πF␈↓↓ ␈↓αa|␈↓↓xx␈↓ and ␈↓↓␈↓πF␈↓↓ ␈↓αd|␈↓↓xx␈↓ and prove from them ␈↓↓␈↓πF␈↓↓ xx␈↓.

␈↓ ↓H␈↓This is essentially what we did in carrying out the above proof.



␈↓ ↓H␈↓3.  ␈↓αSummary of notion of formal theory.␈↓


␈↓ ↓H␈↓        Now␈α⊃we␈α⊃will␈α⊃make␈α⊃precise␈α⊂our␈α⊃notion␈α⊃of␈α⊃first␈α⊃order␈α⊂theory␈α⊃of␈α⊃which␈α⊃the␈α⊃theory␈α⊃of␈α⊂S-
␈↓ ↓H␈↓expressions␈α∞is␈α∞an␈α∂example.␈α∞ As␈α∞mentioned␈α∞before,␈α∂the␈α∞basic␈α∞components␈α∞of␈α∂such␈α∞a␈α∞theory␈α∂are␈α∞a
␈↓ ↓H␈↓domain␈αwith␈αsome␈αdesignated␈αsubdomains␈α(called␈αsorts)␈αa␈αlanguage␈αwhose␈αpredicate␈αand␈αfunction
␈↓ ↓H␈↓symbols␈α
are␈αinterpreted␈α
in␈α
the␈αdomain␈α
and␈α
a␈αcollection␈α
of␈α
facts␈αthat␈α
are␈α
true␈αof␈α
the␈αdomain.␈α
 What
␈↓ ↓H␈↓we␈αneed␈αto␈αdo␈αis␈α
make␈αclear␈αthe␈αprocess␈αused␈αto␈α
describe␈αthe␈αbasic␈αcomponents␈αand␈αto␈α
show␈αhow
␈↓ ↓H␈↓this␈α
description␈α
determines␈α
the␈α
well-formed␈αexpressions␈α
of␈α
the␈α
language,␈α
their␈α
interpretation␈αin␈α
the
␈↓ ↓H␈↓domain of interest and say what the rules for (syntactically) deducing additional facts.

␈↓ ↓H␈↓        We␈α∞assume␈α
that␈α∞the␈α
domain␈α∞of␈α
interest,␈α∞its␈α
subdomains,␈α∞and␈α
a␈α∞collection␈α
of␈α∞functions␈α
and
␈↓ ↓H␈↓predicates␈αon␈αthe␈α
domain␈αare␈αknown␈α
(given␈αby␈αsome␈αindependent␈α
means).␈α  Our␈αdescription␈α
of␈αa
␈↓ ↓H␈↓theory will have the following basic outline:

␈↓ ↓H␈↓(i) List the sort symbols and specify the subdomain denoted by each.

␈↓ ↓H␈↓(ii) Present the domain relations.  (See the discussion of ␈↓αfacts␈↓.)

␈↓ ↓H␈↓(iii) List the constants for each sort, and specify the object denoted by each one.

␈↓ ↓H␈↓(iv) List the variable ranging over each sort.

␈↓ ↓H␈↓(v) List the function symbols, specifying the function denoted.

␈↓ ↓H␈↓(vi) Give domain maps for each function symbol.

␈↓ ↓H␈↓(vii) List the predicate symbols, giving the predicate denoted by each.
␈↓ ↓H␈↓56␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓      Sort symbols are considered to be unary predicate symbols with the obvious denotation.
␈↓ ↓H␈↓      "=" is assumed to be a binary predicate symbol with the standard interpretation

␈↓ ↓H␈↓(viii) Give the axioms.

␈↓ ↓H␈↓The␈α∩non-logical␈α⊃constants␈α∩are␈α∩then␈α⊃the␈α∩sort␈α⊃symbols,␈α∩the␈α∩constant␈α⊃and␈α∩variable␈α∩symbols,␈α⊃the
␈↓ ↓H␈↓function␈αsymbols␈α
and␈αthe␈αadditional␈α
predicate␈αsymbols.␈α If␈α
␈↓πx␈↓␈αis␈αa␈α
variable␈αof␈αsort␈α
␈↓↓D␈↓␈αthen␈α
we␈αwill
␈↓ ↓H␈↓also␈α
use␈α
␈↓πx␈↓␈↓β0␈↓,␈α
␈↓πx␈↓␈↓β1␈↓,␈α
...␈α
as␈α
variables␈α
of␈α
that␈αsort.␈α
 Note␈α
that␈α
each␈α
function␈α
and␈α
predicate␈α
symbol␈α
has␈αa
␈↓ ↓H␈↓fixed␈αnumber␈αof␈α
arguments␈αcalled␈αthe␈α
arity.␈α (This␈αis␈αdetermined␈α
when␈αthe␈αfunction␈α
or␈αpredicate
␈↓ ↓H␈↓denoted by the symbol is specified.)

␈↓ ↓H␈↓        The␈αabove␈αform␈αof␈αdescription␈αdefines␈αthe␈α
basic␈αtheory.␈α This␈αtheory␈αwill␈αbe␈αextended␈α
from
␈↓ ↓H␈↓time to time by adding additional function and predicate symbols and defining axioms.

␈↓ ↓H␈↓α␈↓ ¬dThe Language.

␈↓ ↓H␈↓        Now␈α∩we␈α∪describe␈α∩how␈α∩the␈α∪expressions␈α∩of␈α∪the␈α∩language␈α∩are␈α∪built␈α∩once␈α∪the␈α∩non-logical
␈↓ ↓H␈↓symbols␈α∪have␈α∪been␈α∪given.␈α∪ There␈α∪are␈α∩four␈α∪classes␈α∪of␈α∪expressions:␈α∪ terms,␈α∪formulas,␈α∩function
␈↓ ↓H␈↓expressions,␈α∃and␈α∃predicate␈α∃expressions.␈α⊗ Terms␈α∃denote␈α∃objects␈α∃(individuals)␈α∃in␈α⊗the␈α∃domain,
␈↓ ↓H␈↓formulas␈α
denote␈α
truthvalues,␈α∞function␈α
and␈α
predicate␈α∞expressions␈α
denote␈α
functions␈α∞and␈α
predicates
␈↓ ↓H␈↓on␈αthe␈αdomain.␈α We␈αdefine␈α
the␈αfour␈αclasses␈αof␈αexpressions␈α
inductively.␈α Terms␈αare␈αused␈αin␈α
making
␈↓ ↓H␈↓formulas,␈α∞and␈α∞formulas␈α∞occur␈α∞in␈α∞terms␈α∞so␈α∞that␈α∞the␈α∞definitions␈α∞are␈α∞␈↓↓mutually␈↓␈α∞␈↓↓recursive.␈↓␈α∞ Function
␈↓ ↓H␈↓and predicate expressions are also involved in the mutual recursion.

␈↓ ↓H␈↓␈↓αTerms␈↓:␈α⊂Constants␈α⊂are␈α⊂terms,␈α⊂and␈α⊂variables␈α⊂are␈α∂terms.␈α⊂ If␈α⊂␈↓πy␈↓␈α⊂is␈α⊂a␈α⊂function␈α⊂expression␈α⊂taking␈α∂␈↓↓n␈↓
␈↓ ↓H␈↓arguments,␈αand␈α␈↓↓t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓␈↓␈αare␈αterms,␈αthen␈α␈↓↓␈↓πy␈↓↓[t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓]␈↓␈αis␈αa␈αterm.␈α If␈α␈↓↓A␈↓␈αis␈αa␈αformula␈αand␈α␈↓↓t␈↓β1␈↓↓␈↓␈αand
␈↓ ↓H␈↓␈↓↓t␈↓β2␈↓↓␈↓␈αare␈αterms,␈αthen␈α[␈↓↓IF A THEN t␈↓β1␈↓↓ ELSE t␈↓β2␈↓↓␈↓]␈αis␈αa␈αterm.␈α Some␈αexamples␈αof␈αterms␈αin␈αthe␈αtheory␈αof
␈↓ ↓H␈↓S-expressions are:

␈↓ ↓H␈↓␈↓ ∧8(i)   ␈↓¬NIL␈↓
␈↓ ↓H␈↓␈↓ ∧8(ii)   ␈↓↓x␈↓
␈↓ ↓H␈↓␈↓ ∧8(iii)   ␈↓↓␈↓αa|␈↓↓x␈↓
␈↓ ↓H␈↓␈↓ ∧8(iv)   ␈↓↓IF ATOM x THEN x ELSE ␈↓αd|␈↓↓x␈↓

␈↓ ↓H␈↓␈↓αFunction␈α⊂expressions␈↓:␈α∂A␈α⊂function␈α∂symbol␈α⊂is␈α∂a␈α⊂function␈α∂expression.␈α⊂ If␈α∂␈↓↓␈↓πx␈↓↓␈↓β1␈↓↓, ... ,␈↓πx␈↓↓␈↓βn␈↓↓␈↓␈α⊂are␈α∂general
␈↓ ↓H␈↓variables and ␈↓↓t␈↓ is a term, then ␈↓↓[λ␈↓πx␈↓↓␈↓β1␈↓↓, ... ,␈↓πx␈↓↓␈↓βn␈↓↓: t]␈↓ is a function expression.

␈↓ ↓H␈↓␈↓αPredicate␈α∞expressions␈↓:␈α
A␈α∞predicate␈α
symbol␈α∞is␈α∞a␈α
predicate␈α∞expression.␈α
 If␈α∞␈↓↓␈↓πx␈↓↓␈↓β1␈↓↓, ... ,␈↓πx␈↓↓␈↓βn␈↓↓␈↓␈α∞are␈α
general
␈↓ ↓H␈↓variables and ␈↓↓A␈↓ is a formula, then ␈↓↓[λ␈↓πx␈↓↓␈↓β1␈↓↓, ... ,␈↓πx␈↓↓␈↓βn␈↓↓: A]␈↓ is a predicate expression.

␈↓ ↓H␈↓Some examples of function and predicate expressions are:

␈↓ ↓H␈↓␈↓ ∧8(v)   ␈↓αa␈↓
␈↓ ↓H␈↓␈↓ ∧8(vi)   ␈↓↓SEXP␈↓
␈↓ ↓H␈↓␈↓ ∧8(vii)   ␈↓↓λX: IF ATOM X THEN X ELSE ␈↓αd|␈↓↓X␈↓

␈↓ ↓H␈↓␈↓αFormulas␈↓:␈α⊂If␈α⊃␈↓πf␈↓␈α⊂is␈α⊃a␈α⊂predicate␈α⊃expression␈α⊂taking␈α⊂␈↓↓n␈↓␈α⊃arguments␈α⊂and␈α⊃␈↓↓t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓␈↓␈α⊂are␈α⊃terms,␈α⊂then
␈↓ ↓H␈↓␈↓↓␈↓πf␈↓↓[t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓]␈↓␈αis␈αa␈αformula.␈α In␈αparticular,␈αif␈α␈↓↓t␈↓β1␈↓↓␈↓␈αand␈α␈↓↓t␈↓β2␈↓↓␈↓␈αare␈αterms␈αthen␈α␈↓↓t␈↓β1␈↓↓ = t␈↓β2␈↓↓␈↓␈αis␈αa␈αformula.␈α If␈α␈↓↓A,␈↓
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *57


␈↓ ↓H␈↓␈↓↓B,␈↓␈α∪␈↓↓C␈↓␈α∪are␈α∪formulas,␈α∪then␈α∪␈↓↓¬A␈↓,␈α∪␈↓↓A ∧ B␈↓,␈α∪␈↓↓A ∨ B␈↓,␈α∪␈↓↓A ⊃ B␈↓,␈α∪␈↓↓S ≡ B␈↓,␈α∪and␈α∪␈↓↓IF A THEN B ELSE C␈↓␈α∩are
␈↓ ↓H␈↓formulas.␈α If␈α
␈↓↓␈↓πx␈↓↓␈↓β1␈↓↓, ..., ␈↓πx␈↓↓␈↓βn␈↓↓␈↓␈αare␈α
variables,␈αand␈α
␈↓↓A␈↓␈αis␈α
a␈αformula,␈α
then␈α␈↓↓[∃␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: A]␈↓␈αand␈α
␈↓↓[∀␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: A]␈↓
␈↓ ↓H␈↓are formulas.  Some examples of formulas in the theory of S-expressions are:

␈↓ ↓H␈↓␈↓ ∧8(viii)   ␈↓↓␈↓αa|␈↓↓x = ␈↓αd|␈↓↓x␈↓
␈↓ ↓H␈↓␈↓ ∧8(ix)   ␈↓↓ATOM X␈↓
␈↓ ↓H␈↓␈↓ ∧8(x)   ␈↓↓IF ¬ATOM x THEN ␈↓αa|␈↓↓x = ␈↓αd|␈↓↓x ELSE ␈↓¬T␈↓↓ = ␈↓¬T␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧8(xi)   ␈↓↓∀x: [PAIR x ⊃ ∃y z: x = y . z]␈↓


␈↓ ↓H␈↓        An␈αoccurrence␈αof␈αa␈αvariable␈α␈↓πx␈↓␈αis␈αclassified␈αas␈α␈↓↓bound␈↓␈αor␈α␈↓↓free␈↓␈αaccording␈αto␈αthe␈αfollowing␈αrule.
␈↓ ↓H␈↓␈↓πx␈↓␈α
is␈α∞bound␈α
in␈α
an␈α∞expression␈α
of␈α
one␈α∞of␈α
the␈α
forms␈α∞␈↓↓[λ␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: t]␈↓,␈α
␈↓↓[λ␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: s]␈↓,␈α∞␈↓↓[∀␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: s]␈↓␈α
or
␈↓ ↓H␈↓␈↓↓[∃␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: s]␈↓␈αif␈αit␈αis␈αone␈αof␈αthe␈αnumbered␈α␈↓πx␈↓'s.␈α An␈αoccurrence␈αof␈α␈↓πx␈↓␈αin␈αan␈αexpression␈α␈↓↓e␈↓␈αis␈αbound␈αif
␈↓ ↓H␈↓that␈αoccurence␈αis␈αbound␈αin␈αsome␈αsubexpression␈αof␈α␈↓↓e.␈↓␈α Otherwise␈αit␈αis␈αfree.␈α A␈αformula␈αhaving␈αno
␈↓ ↓H␈↓free␈α∞variables␈α∞is␈α∞called␈α∞a␈α∞␈↓↓sentence.␈↓␈α∞ Thus␈α∞␈↓↓x␈↓␈α∞is␈α∞bound␈α∞in␈α∞example␈α∞(iv)␈α∞and␈α∞(vi)␈α∞ but␈α∞not␈α∞in␈α∞(iii).
␈↓ ↓H␈↓The formula of example (xi) is a sentence.

␈↓ ↓H␈↓        We␈α∂note␈α∂that␈α∂conditional␈α∞expressions␈α∂and␈α∂λ-expressions␈α∂are␈α∞not␈α∂ordinarily␈α∂included␈α∂in␈α∞a
␈↓ ↓H␈↓first␈αorder␈αlanguage.␈α
 These␈αextensions␈αdo␈α
not␈αchange␈αthe␈αlogical␈α
strength␈αof␈αthe␈α
theory,␈αbecause,
␈↓ ↓H␈↓as␈α⊃we␈α⊃shall␈α∩see,␈α⊃every␈α⊃formula␈α∩that␈α⊃includes␈α⊃conditional␈α∩expressions␈α⊃or␈α⊃λ-expressions␈α∩can␈α⊃be
␈↓ ↓H␈↓transformed␈α∂into␈α∂an␈α∞equivalent␈α∂formula␈α∂without␈α∂them.␈α∞ However,␈α∂the␈α∂extensions␈α∂are␈α∞practically
␈↓ ↓H␈↓important, because they permit us to use recursive definitions directly as formulas of the logic.

␈↓ ↓H␈↓α␈↓ ∧\Assigning meaning to expressions.

␈↓ ↓H␈↓        Next␈αwe␈αexplain␈αhow␈αto␈αdetermine␈αthe␈αmeaning␈αof␈αan␈αexpression␈αin␈αthe␈αdomain␈αof␈αinterest
␈↓ ↓H␈↓(or␈α∂any␈α∂domain␈α∂where␈α∂the␈α∂non-logical␈α∂symbols␈α∂have␈α∂been␈α∂assigned␈α∂an␈α∂appropriate␈α∂meaning).  
␈↓ ↓H␈↓The␈αmeaning␈αof␈αexpressions␈αcontaining␈αfree␈αvariables␈αcan␈αnot␈αbe␈αdetermined,␈αin␈αgeneral,␈αwithout
␈↓ ↓H␈↓assigning␈αa␈αvalue␈α
to␈αeach␈αof␈α
the␈αvariables.␈α We␈αsay␈α
that␈αan␈αassignment␈α
is␈α"allowed"␈αif␈αthe␈α
variable
␈↓ ↓H␈↓is␈αgeneral␈α
or␈αif␈αit␈α
ranges␈αover␈αa␈α
subdomain␈α␈↓↓D␈↓␈αand␈α
the␈αvalue␈αassigned␈α
is␈αin␈αthat␈α
subdomain.␈α So,
␈↓ ↓H␈↓we␈αwill␈αimagine␈αa␈α
fixed␈αbut␈αarbitrary␈αinterpretation␈αassigning␈α
allowed␈αvalues␈αto␈αeach␈α
variable␈αof
␈↓ ↓H␈↓the␈α
language.␈α
 Meanings␈α
are␈α
then␈α
relative␈α
to␈α
such␈α
and␈α
interpretation.␈α
 Meanings␈α∞are␈α
determined
␈↓ ↓H␈↓inductively in a manner parallel to the construction of expressions.

␈↓ ↓H␈↓␈↓αTerms␈↓:␈αThe␈α
meaning␈αof␈α
a␈αconstant␈α
is␈αthe␈αobject␈α
that␈αit␈α
denotes.␈α The␈α
meaning␈αof␈α
a␈αvariable␈αis␈α
the
␈↓ ↓H␈↓value␈αassigned␈αto␈αit␈αby␈α the␈αinterpretation.␈α If␈α␈↓πy␈↓␈αis␈αa␈αfunction␈αexpression␈αtaking␈α␈↓↓n␈↓␈αarguments,␈αand
␈↓ ↓H␈↓␈↓↓t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓␈↓␈α
are␈α
terms,␈α
then␈α
the␈α
meaning␈α
of␈α
␈↓↓␈↓πy␈↓↓[t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓]␈↓␈α
is␈α
the␈α
value␈α
of␈α
the␈α
function␈α
assigned␈αto␈α
␈↓πy␈↓
␈↓ ↓H␈↓when␈αapplied␈αto␈αthe␈αvalues␈α
assigned␈αto␈αthe␈αterms␈α␈↓↓t␈↓βi␈↓↓.␈α
 If␈α␈↓A␈αis␈αa␈αformula␈α
and␈α␈↓↓t␈↓β1␈↓↓␈↓␈αand␈α␈↓↓t␈↓β2␈↓↓␈↓␈αare␈αterms,␈α
if
␈↓ ↓H␈↓␈↓↓A␈↓␈α
is␈α
true␈α
then␈α
the␈α
value␈α
assigned␈αto␈α
␈↓↓IF A THEN t␈↓β1␈↓↓ ELSE t␈↓β2␈↓↓␈↓␈α
is␈α
the␈α
value␈α
assigned␈α
to␈α␈↓↓t␈↓β1␈↓↓␈↓,␈α
otherwise
␈↓ ↓H␈↓it is assigned the value assigned to ␈↓↓t␈↓β2␈↓↓␈↓.

␈↓ ↓H␈↓In␈α∞the␈α∞theory␈α
of␈α∞S-expressions,␈α∞if␈α∞the␈α
value␈α∞assigned␈α∞to␈α∞␈↓↓x␈↓␈α
is␈α∞␈↓¬(A␈α∞.␈α∞B)␈↓␈α
then␈α∞the␈α∞meanings␈α∞of␈α
the
␈↓ ↓H␈↓terms given in examples (i)-(iv) above are:

␈↓ ↓H␈↓␈↓ ∧8(i)   ␈↓¬NIL␈↓
␈↓ ↓H␈↓␈↓ ∧8(ii)   ␈↓¬(A . B)␈↓
␈↓ ↓H␈↓␈↓ ∧8(iii)   ␈↓¬A ␈↓
␈↓ ↓H␈↓␈↓ ∧8(iv)   ␈↓¬B ␈↓
␈↓ ↓H␈↓58␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓␈↓αFunction␈αand␈α
predicate␈αexpressions␈↓:␈α
Function␈αand␈α
predicate␈αsymbols␈α
are␈αassigned␈α
the␈αfunction␈α
or
␈↓ ↓H␈↓predicate␈α∩denoted␈α⊃by␈α∩the␈α⊃symbol.␈α∩ The␈α∩expression␈α⊃␈↓↓[λ␈↓πx␈↓↓␈↓β1␈↓↓, ... ,␈↓πx␈↓↓␈↓βn␈↓↓: e]␈↓␈α∩is␈α⊃assigned␈α∩a␈α∩function␈α⊃or
␈↓ ↓H␈↓predicate␈αaccording␈αto␈αwhether␈α␈↓↓e␈↓␈αis␈αa␈αterm␈αor␈αformula.␈α The␈αvalue␈αof␈α␈↓↓[λ␈↓πx␈↓↓␈↓β1␈↓↓, ... ,␈↓πx␈↓↓␈↓βn␈↓↓: e][t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓]␈↓
␈↓ ↓H␈↓the␈αvalue␈αassigned␈αto␈α␈↓↓e␈↓␈αrelative␈αto␈αan␈αinterpretation␈αwhich␈αis␈αmodified␈αby␈αassigning␈αthe␈αvalues␈αof
␈↓ ↓H␈↓the ␈↓↓t␈↓s to the corresponding ␈↓πx␈↓s.

␈↓ ↓H␈↓This␈α⊃is␈α⊃our␈α⊃reason␈α⊃for␈α⊃only␈α∩allowing␈α⊃λ-binding␈α⊃of␈α⊃general␈α⊃variables.␈α⊃ Namely␈α⊃if␈α∩we␈α⊃allowed
␈↓ ↓H␈↓binding␈α
of␈α
sorted␈α
variables␈α
then␈α
some␈α
terms␈α
would␈α
not␈α
have␈α
well␈α
defined␈α
values,␈α
and␈α
this␈α
is␈αnot␈α
in
␈↓ ↓H␈↓the␈α
spirit␈α
of␈α
first␈α∞order␈α
logic.␈α
  Another␈α
approach␈α
would␈α∞be␈α
to␈α
allow␈α
λ-binding␈α
of␈α∞any␈α
variable,
␈↓ ↓H␈↓but␈α
only␈α
allow␈αapplication␈α
of␈α
such␈αexpressions␈α
to␈α
terms␈αof␈α
the␈α
appropriate␈αsort.␈α
 This␈α
makes␈αthe
␈↓ ↓H␈↓class␈α∂of␈α∂well-formed␈α∂expressions␈α∂undecidable␈α∂which␈α∂is␈α∂undesirable␈α∂if␈α∂we␈α∂wish␈α∂to␈α∂represent␈α∂our
␈↓ ↓H␈↓system is in a computer.

␈↓ ↓H␈↓␈↓αFormulas␈↓:␈α⊂If␈α⊃␈↓πf␈↓␈α⊂is␈α⊃a␈α⊂predicate␈α⊃expression␈α⊂taking␈α⊂␈↓↓n␈↓␈α⊃arguments␈α⊂and␈α⊃␈↓↓t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓␈↓␈α⊂are␈α⊃terms,␈α⊂then
␈↓ ↓H␈↓␈↓↓␈↓πf␈↓↓[t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓]␈↓␈α∞is␈α∞true␈α∂exactly␈α∞when␈α∞the␈α∞tuple␈α∂of␈α∞values␈α∞assigned␈α∞to␈α∂the␈α∞␈↓↓t␈↓s␈α∞satisfies␈α∂the␈α∞predicate
␈↓ ↓H␈↓assigned␈αto␈α␈↓πf␈↓.␈α In␈αparticular,␈αif␈α␈↓↓t␈↓β1␈↓↓␈↓␈αand␈α␈↓↓t␈↓β2␈↓↓␈↓␈αare␈αterms␈αthen␈α␈↓↓t␈↓β1␈↓↓ = t␈↓β2␈↓↓␈↓␈αis␈αtrue␈αexactly␈αwhen␈α␈↓↓t␈↓β1␈↓↓␈↓␈αand␈α␈↓↓t␈↓β2␈↓↓␈↓␈αare
␈↓ ↓H␈↓assigned␈α∂the␈α∂same␈α∂value.␈α⊂ If␈α∂␈↓↓A,␈↓␈α∂␈↓↓B,␈↓␈α∂␈↓↓C␈↓␈α⊂are␈α∂formulas,␈α∂then␈α∂␈↓↓¬A␈↓,␈α∂is␈α⊂true␈α∂exactly␈α∂when␈α∂␈↓↓A␈↓␈α⊂is␈α∂false,
␈↓ ↓H␈↓␈↓↓IF A THEN B ELSE C␈↓␈αis␈αtrue␈αif␈α␈↓↓A␈↓␈αis␈αtrue␈αand␈α␈↓↓B␈↓␈αis␈αtrue␈αor␈αif␈α␈↓↓A␈↓␈αis␈αfalse␈αand␈α␈↓↓C␈↓␈αis␈αtrue␈αand␈αfalse
␈↓ ↓H␈↓otherwise.␈α∪ The␈α∪values␈α∩of␈α∪␈↓↓A ∧ B␈↓,␈α∪␈↓↓A ∨ B␈↓,␈α∩␈↓↓A ⊃ B␈↓,␈α∪and␈α∪␈↓↓A ≡ B␈↓␈α∩are␈α∪determined␈α∪from␈α∪the␈α∩values
␈↓ ↓H␈↓assigned to ␈↓↓A␈↓ and ␈↓↓B␈↓ according to the following truthtable:

␈↓ ↓H␈↓␈↓ ∧λ␈↓↓A␈↓␈↓ ¬λ␈↓↓B␈↓␈↓ ελ∧␈↓ πλ∨␈↓ λλ⊃␈↓ 	λ≡

␈↓ ↓H␈↓␈↓ βx␈↓αtrue␈↓␈↓ ∧x␈↓αtrue␈↓␈↓ ¬x␈↓αtrue␈↓␈↓ εx␈↓αtrue␈↓␈↓ πx␈↓αtrue␈↓␈↓ λx␈↓αtrue␈↓
␈↓ ↓H␈↓␈↓ βx␈↓αtrue␈↓␈↓ ∧x␈↓αfalse␈↓␈↓ ¬x␈↓αfalse␈↓␈↓ εx␈↓αtrue␈↓␈↓ πx␈↓αfalse␈↓␈↓ λx␈↓αfalse␈↓
␈↓ ↓H␈↓␈↓ βx␈↓αfalse␈↓␈↓ ∧x␈↓αtrue␈↓␈↓ ¬x␈↓αfalse␈↓␈↓ εx␈↓αtrue␈↓␈↓ πx␈↓αtrue␈↓␈↓ λx␈↓αfalse␈↓
␈↓ ↓H␈↓␈↓ βx␈↓αfalse␈↓␈↓ ∧x␈↓αfalse␈↓␈↓ ¬x␈↓αfalse␈↓␈↓ εx␈↓αfalse␈↓␈↓ πx␈↓αtrue␈↓␈↓ λx␈↓αtrue␈↓


␈↓ ↓H␈↓␈↓ ∧~␈↓αTable 4.␈↓  The semantics of logical connectives.


␈↓ ↓H␈↓If␈α␈↓↓␈↓πx␈↓↓␈↓β1␈↓↓, ..., ␈↓πx␈↓↓␈↓βn␈↓↓␈↓␈α
are␈αvariables,␈α
and␈α␈↓↓A␈↓␈α
is␈αa␈αformula,␈α
then␈α␈↓↓[∀␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: A]␈↓␈α
is␈αtrue␈α
exactly␈αif␈α
␈↓↓A␈↓␈αis␈αtrue␈α
for
␈↓ ↓H␈↓all␈αallowed␈αinterpretations␈αof␈αthe␈αvariables␈αdiffering␈αfrom␈αthe␈αfixed␈αone␈αonly␈αin␈αassignments␈α
made
␈↓ ↓H␈↓to␈α∞the␈α∂variables␈α∞␈↓πx␈↓␈↓βi␈↓.␈α∞ ␈↓↓[∃␈↓πx␈↓↓␈↓β1␈↓↓ ... ␈↓πx␈↓↓␈↓βn␈↓↓: A]␈↓␈α∂is␈α∞true␈α∞exactly␈α∂if␈α∞there␈α∞is␈α∂some␈α∞allowed␈α∞interpretation␈α∂of␈α∞the
␈↓ ↓H␈↓variables differing from the fixed one only in assignments made to the variables ␈↓πx␈↓␈↓βi␈↓.

␈↓ ↓H␈↓In␈α∞the␈α∞theory␈α
of␈α∞S-expressions,␈α∞if␈α∞the␈α
value␈α∞assigned␈α∞to␈α∞␈↓↓x␈↓␈α
is␈α∞␈↓¬(A␈α∞.␈α∞B)␈↓␈α
then␈α∞the␈α∞meanings␈α∞of␈α
the
␈↓ ↓H␈↓formulas given in examples (viii)-(x) above are:

␈↓ ↓H␈↓␈↓ ∧8(viii)   ␈↓αfalse␈↓
␈↓ ↓H␈↓␈↓ ∧8(ix)   ␈↓αfalse␈↓
␈↓ ↓H␈↓␈↓ ∧8(x)   ␈↓αfalse␈↓

␈↓ ↓H␈↓while␈αif␈αthe␈αvalue␈αassigned␈αto␈α␈↓↓x␈↓␈αis␈α␈↓¬(A␈α.␈αA)␈↓␈αthen␈αthe␈αmeanings␈αof␈αthe␈αformulas␈αgiven␈αin␈αexamples
␈↓ ↓H␈↓(viii)-(x) above are:
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *59



␈↓ ↓H␈↓␈↓ ∧8(viii)   ␈↓αtrue␈↓
␈↓ ↓H␈↓␈↓ ∧8(ix)   ␈↓αfalse␈↓
␈↓ ↓H␈↓␈↓ ∧8(x)   ␈↓αtrue␈↓

␈↓ ↓H␈↓The␈α
formula␈α
of␈α
example␈α
(xi)␈α
is␈α
true␈α
for␈α
any␈α
interpration␈α
of␈α
the␈α
variables.␈α
 Being␈α
a␈α
sentence,␈α
its
␈↓ ↓H␈↓truth or falsity doesn't depend on the values assigned to the variables.

␈↓ ↓H␈↓        Those␈α
who␈αare␈α
familiar␈αwith␈α
the␈α
lambda␈αcalculus␈α
should␈αnote␈α
that␈α
λ␈αis␈α
being␈αused␈α
here␈αin␈α
a
␈↓ ↓H␈↓very␈α
limited␈α
way.␈α Namely,␈α
the␈α
variables␈αin␈α
a␈α
lambda-expression␈α
take␈αonly␈α
elements␈α
of␈αthe␈α
domain
␈↓ ↓H␈↓as␈α⊂values,␈α⊂whereas␈α∂the␈α⊂essence␈α⊂of␈α∂the␈α⊂lambda␈α⊂calculus␈α∂is␈α⊂that␈α⊂they␈α∂take␈α⊂arbitrary␈α⊂functions␈α∂as
␈↓ ↓H␈↓values.  We may call these restricted lambda expressions ␈↓↓first-order lambdas␈↓.

␈↓ ↓H␈↓α␈↓ ε%Facts

␈↓ ↓H␈↓        Facts␈α∞come␈α
in␈α∞two␈α
flavors:␈α∞domain␈α
facts␈α∞and␈α
axioms.␈α∞ Axioms␈α
are␈α∞simply␈α
sentences␈α∞of␈α
the
␈↓ ↓H␈↓language␈α⊂that␈α∂we␈α⊂are␈α∂asserting␈α⊂to␈α∂be␈α⊂true␈α⊂in␈α∂the␈α⊂domain.␈α∂ There␈α⊂are␈α∂three␈α⊂way␈α⊂of␈α∂expression
␈↓ ↓H␈↓domain␈αfacts:␈αas␈αdomain␈αrelations,␈αas␈αdomain␈αmembership␈αspecifications␈αand␈αas␈αdomain␈αmappings
␈↓ ↓H␈↓for␈αfunctions.␈α The␈αuse␈αof␈α
domain␈αexpressions␈αis␈αa␈αmatter␈α
using␈αa␈αmore␈αnatural␈αnotation.␈α
 As␈αwe
␈↓ ↓H␈↓will see each expression of a domain fact has an equivalent axiom.

␈↓ ↓H␈↓        Domain␈αterms␈αare␈αformed␈αfrom␈αsort␈αsymbols␈αand␈αfinite␈αsubdomain␈αdescriptions␈αof␈αthe␈αform
␈↓ ↓H␈↓{␈↓πz␈↓␈↓β1␈↓, ... ,␈↓πz␈↓␈↓βn␈↓}␈αusing␈αthe␈αbinary␈αboolean␈αoperations␈α∪␈αand␈α∩␈αmeaning␈αunion␈αand␈αintersection.␈α Domain
␈↓ ↓H␈↓relations␈α⊂are␈α∂formed␈α⊂from␈α∂domain␈α⊂terms␈α∂using␈α⊂the␈α∂binary␈α⊂relations␈α∂symbols␈α⊂=␈α∂and␈α⊂⊂␈α∂meaning
␈↓ ↓H␈↓equality␈α
and␈α
containment.␈α
 The␈αaxioms␈α
corresponding␈α
to␈α
the␈α
domain␈αrelations␈α
for␈α
the␈α
theory␈αof␈α
S-
␈↓ ↓H␈↓expressions are:

␈↓ ↓H␈↓␈↓ αX␈↓↓SEXP = ATOM ∪ PAIR␈↓␈↓ ¬h↔␈↓ πλ␈↓↓∀X: [SEXP X ≡ ATOM X ∨ PAIR X]␈↓
␈↓ ↓H␈↓␈↓ αX␈↓↓ATOM ∪ PAIR = ␈↓πf␈↓↓␈↓␈↓ ¬h↔␈↓ πλ␈↓↓∀X: ¬[ATOM X ∧ PAIR X]␈↓

␈↓ ↓H␈↓Domain membership facts have the form:

␈↓ ↓H␈↓␈↓ ¬l␈↓↓␈↓πz␈↓↓ ε D ␈↓or␈↓↓ ␈↓πx␈↓↓ ε D␈↓ 

␈↓ ↓H␈↓where␈α
␈↓πz␈↓␈α
denotes␈α
a␈α
constant␈α
symbol,␈α
␈↓πx␈↓␈α
denotes␈α
a␈α
variable␈α
symbol␈α
and␈α
␈↓↓D␈↓␈α
denotes␈α
a␈α
sort␈αsymbol.␈α
 The
␈↓ ↓H␈↓corresponding axioms are:

␈↓ ↓H␈↓␈↓ ¬c␈↓↓D ␈↓πz␈↓↓ ␈↓or␈↓↓ ∀␈↓πx␈↓↓: D ␈↓πx␈↓↓.␈↓ 

␈↓ ↓H␈↓Function mappings have the general form:

␈↓ ↓H␈↓␈↓ ¬5␈↓↓F: D␈↓β0␈↓↓ ␈↓¬x␈↓↓ ... ␈↓¬x␈↓↓ D␈↓βn␈↓↓ → D␈↓β0␈↓↓␈↓ 

␈↓ ↓H␈↓which corresponds to  the axiom:

␈↓ ↓H␈↓␈↓ βa␈↓↓∀X␈↓β1␈↓↓ ... X␈↓βn␈↓↓: [D␈↓β1␈↓↓ X␈↓β1␈↓↓ ∧ ... ∧ D␈↓βn␈↓↓ X␈↓βn␈↓↓] ⊃ D␈↓β0␈↓↓ F[X␈↓β1␈↓↓ ... X␈↓βn␈↓↓]]␈↓ 

␈↓ ↓H␈↓where the D␈↓βi␈↓ denote sort symbols and F denotes a function symbol taking ␈↓↓n␈↓ arguments.
␈↓ ↓H␈↓60␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓α␈↓ ε#Rules

␈↓ ↓H␈↓        There␈αare␈αmany␈αpossible␈αformal␈αproof␈αsystems␈α(collections␈αof␈αrules␈αfor␈αdeducing␈αfacts).␈α The
␈↓ ↓H␈↓natural␈αdeduction␈αsystem␈αmentioned␈αin␈α␈↓π∞␈↓␈α2␈αis␈αone␈αpossibility.␈α  Basically␈αany␈αrule␈αthat␈αonly␈αallows
␈↓ ↓H␈↓deduction␈α
from␈αgiven␈α
facts␈αof␈α
facts␈αthat␈α
are␈αtrue␈α
in␈αany␈α
domain␈αwhere␈α
all␈αof␈α
the␈αgiven␈α
facts␈αare
␈↓ ↓H␈↓true␈α
is␈α
a␈α
valid␈α
rule.␈α
 We␈α
will␈α
not␈α
specify␈α
any␈α
particular␈α
proof␈α
rules.␈α
 However,␈α
the␈α
proofs␈α
presented
␈↓ ↓H␈↓will␈αbe,␈αfor␈αthe␈α
most␈αpart,␈αinformal␈αversions␈αof␈α
"natural"␈αdeduction␈αproofs␈αaugmented␈αby␈α
extended
␈↓ ↓H␈↓substitution␈α
rules␈α
and␈αtautologies.␈α
  In␈α
fact␈αmost␈α
of␈α
the␈αproofs␈α
have␈α
been␈αcarried␈α
out␈α
formally␈αin
␈↓ ↓H␈↓such␈α
a␈α
system␈α∞and␈α
confirmed␈α
by␈α∞a␈α
proof-checker.␈α
 Since␈α
the␈α∞use␈α
of␈α
sorted␈α∞variables,␈α
conditional
␈↓ ↓H␈↓expressions and first order lambdas are not standard, we give some rules below for treating them.


␈↓ ↓H␈↓αManipulating quantifiers over sorted variables.

␈↓ ↓H␈↓        In␈α∨order␈α∨to␈α∨shorten␈α≡formulas,␈α∨we␈α∨use␈α∨sorted␈α≡variables.␈α∨ Thus␈α∨we␈α∨can␈α≡write
␈↓ ↓H␈↓␈↓↓∀xx:[␈↓αa|␈↓↓xx . ␈↓αd|␈↓↓xx = xx)␈↓,␈α∂taking␈α∂advantage␈α∂of␈α∂the␈α∂fact␈α∂that␈α∂the␈α∂variable␈α∂␈↓↓xx␈↓␈α∂ranges␈α∂only␈α∂over␈α∞non-
␈↓ ↓H␈↓atomic␈α∞S-expressions,␈α
instead␈α∞of␈α∞having␈α
to␈α∞write␈α
␈↓↓∀x.[¬ATOM x ⊃ ␈↓αa|␈↓↓x . ␈↓αd|␈↓↓x = x]␈↓.␈α∞ The␈α∞proofs␈α
are
␈↓ ↓H␈↓also shortened, since we can avoid having to prove ␈↓↓¬ATOM xx␈↓ every time the fact is needed.

␈↓ ↓H␈↓        The␈αusual␈αrules␈αgiven␈αfor␈αmanipulating␈αquantifiers␈α(generalization,␈αspecializtion,␈αetc.)␈αapply
␈↓ ↓H␈↓to␈α
variables␈α
that␈α
range␈α
over␈α
the␈α
entire␈α
domain.␈α
 In␈α
order␈α
to␈α
handle␈α
variables␈α
restricted␈α
to␈αrange
␈↓ ↓H␈↓over␈α
some␈α
subset␈α
we␈α
must␈α
either␈α
modify␈α
the␈α
rules␈α
or␈α
show␈α
how␈α
to␈α
eliminate␈α
the␈αrestricted␈α
variables
␈↓ ↓H␈↓from␈α
a␈α
formula.␈α
 Since␈αwe␈α
have␈α
given␈α
no␈αrules␈α
to␈α
modify,␈α
we␈αwill␈α
indicate␈α
how␈α
quantifiers␈αover
␈↓ ↓H␈↓sorted␈α
variables␈α
can␈α
be␈α
eliminated.␈α
 Given␈α
a␈α
particular␈αset␈α
of␈α
rules,␈α
it␈α
would␈α
be␈α
better␈α
to␈αextend
␈↓ ↓H␈↓them␈αto␈αhandle␈αsorted␈αvariables␈αusing␈αthe␈αelimination␈αrules␈αas␈αa␈αguide.␈α Otherwise␈αthe␈αpurpose␈αof
␈↓ ↓H␈↓introducing them is defeated.

␈↓ ↓H␈↓        Suppose␈αthe␈α
variable␈α␈↓πx␈↓␈αis␈α
restricted␈αto␈αrange␈α
over␈αthe␈αsubdomain␈α
␈↓↓D␈↓␈αand␈αthe␈α
variable␈α␈↓πx␈↓␈↓β1␈↓␈αis␈α
a
␈↓ ↓H␈↓general␈α∞variable.␈α
 For␈α∞example,␈α∞in␈α
the␈α∞theory␈α
of␈α∞S-expressions␈α∞we␈α
have␈α∞the␈α∞subdomain␈α
␈↓↓SEXP,␈↓
␈↓ ↓H␈↓with␈αthe␈αvariable␈α␈↓↓x ε SEXP␈↓␈αand␈α␈↓↓X␈↓␈αis␈αa␈αgeneral␈αvariable.␈α The␈αformula␈α␈↓↓∀␈↓πx␈↓↓:A␈↓␈αis␈αthen␈αequivalent␈αto
␈↓ ↓H␈↓␈↓↓∀␈↓πx␈↓↓␈↓β1␈↓↓:[D ␈↓πx␈↓↓␈↓β1␈↓↓ ⊃ s]␈↓ and the formual ␈↓↓∃␈↓πx␈↓↓:A␈↓ is equivalent to ␈↓↓∃␈↓πx␈↓↓␈↓β1␈↓↓.[D ␈↓πx␈↓↓␈↓β1␈↓↓ ∧ A]␈↓.


␈↓ ↓H␈↓αRules for Conditional Expressions.

␈↓ ↓H␈↓        All the properties we shall use of conditional terms follow from the relation

␈↓ ↓H␈↓3.1)␈↓ αA␈↓↓[A ⊃ [IF A THEN t␈↓β1␈↓↓ ELSE t␈↓β2␈↓↓] = t␈↓β1␈↓↓] ∧ [¬A ⊃ [IF A THEN t␈↓β1␈↓↓ ELSE t␈↓β2␈↓↓] = t␈↓β2␈↓↓] ␈↓. 

␈↓ ↓H␈↓        It␈α
is␈α∞worthwhile␈α
to␈α
list␈α∞separately␈α
some␈α
properties␈α∞of␈α
conditional␈α
terms.␈α∞ First␈α
we␈α∞have␈α
the
␈↓ ↓H␈↓obvious

␈↓ ↓H␈↓␈↓ ∧6␈↓↓[IF ␈↓αtrue␈↓↓ THEN t␈↓β1␈↓↓ ELSE t␈↓β2␈↓↓] = t␈↓β1␈↓↓           ␈↓
␈↓ ↓H␈↓3.2)
␈↓ ↓H␈↓␈↓ ∧4␈↓↓[IF ␈↓αfalse␈↓↓ THEN t␈↓β1␈↓↓ ELSE t␈↓β2␈↓↓] = t␈↓β2␈↓↓.         ␈↓

␈↓ ↓H␈↓Next we have a ␈↓↓distributive␈↓ ␈↓↓law␈↓ for functions applied to conditional terms, namely

␈↓ ↓H␈↓3.3)␈↓ β8␈↓↓f[IF A THEN t␈↓β1␈↓↓ ELSE t␈↓β2␈↓↓] = IF A THEN f[t␈↓β1␈↓↓] ELSE f[t␈↓β2␈↓↓] ␈↓. 
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *61


␈↓ ↓H␈↓This␈α
applies␈αto␈α
predicates␈αas␈α
well␈αas␈α
functions␈αand␈α
can␈α
also␈αbe␈α
used␈αwhen␈α
one␈αof␈α
the␈αarguments␈α
of
␈↓ ↓H␈↓a␈αfunction␈αof␈αseveral␈αarguments␈αis␈αa␈αconditional␈αterm.␈α It␈αalso␈αapplies␈αwhen␈αone␈αof␈αthe␈αterms␈αof␈αa
␈↓ ↓H␈↓conditional term is itself a conditional term.

␈↓ ↓H␈↓Thus

␈↓ ↓H␈↓3.4)␈↓ α0␈↓↓IF [IF A THEN B ELSE C] THEN t␈↓β1␈↓↓ ELSE t␈↓β2␈↓↓ =                                        ␈↓
␈↓ ↓H␈↓␈↓ αb␈↓↓IF A THEN [IF B THEN t␈↓β1␈↓↓ ELSE t␈↓β2␈↓↓] ELSE [IF C THEN t␈↓β1␈↓↓ ELSE t␈↓β2␈↓↓]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ αε␈↓↓IF A THEN [IF B THEN t␈↓β1␈↓↓ ELSE t␈↓β2␈↓↓] ELSE t␈↓β3␈↓↓ =                                                ␈↓
␈↓ ↓H␈↓3.5)␈↓ α)␈↓↓IF B THEN [IF A THEN t␈↓β1␈↓↓ ELSE t␈↓β3␈↓↓] ELSE [IF A THEN t␈↓β2␈↓↓ ELSE t␈↓β3␈↓↓] .         ␈↓

␈↓ ↓H␈↓        When␈α∩the␈α∩expressions␈α∩following␈α∩THEN␈α∩and␈α∩ELSE␈α∩are␈α∩formulas,␈α∩then␈α∪the␈α∩conditional
␈↓ ↓H␈↓expression can be replaced by a formula according to

␈↓ ↓H␈↓3.6)␈↓ ∧λ␈↓↓[IF A THEN B ELSE C] ≡ [A ∧ B] ∨ [¬A ∧ C] ␈↓. 

␈↓ ↓H␈↓These␈αrules␈αpermit␈αeliminating␈αconditional␈αexpressions␈αfrom␈αformulas␈αby␈αfirst␈αusing␈αdistributivity
␈↓ ↓H␈↓to␈α∂move␈α∂the␈α⊂conditionals␈α∂to␈α∂the␈α∂outside␈α⊂of␈α∂any␈α∂functions␈α∂or␈α⊂predicates␈α∂and␈α∂then␈α⊂replacing␈α∂the
␈↓ ↓H␈↓conditional␈α⊃expression␈α⊃by␈α⊃a␈α⊃Boolean␈α⊃expression.␈α⊃  They␈α⊃could␈α⊃be␈α⊃added␈α⊃to␈α⊃our␈α⊃formal␈α⊃proof
␈↓ ↓H␈↓system either as rules or as axiom schemata.

␈↓ ↓H␈↓        More␈αfacts␈αabout␈αconditional␈αterms␈αare␈αgiven␈αin␈α[McCarthy␈α1963]␈αincluding␈αa␈αdiscussion␈αof
␈↓ ↓H␈↓canonical␈αforms␈αthat␈αparallels␈αthe␈αcanonical␈αforms␈αof␈αboolean␈αterms.␈α Any␈αquestion␈αof␈αequivalence
␈↓ ↓H␈↓of␈α
conditional␈α∞terms␈α
is␈α
decidable␈α∞by␈α
truth␈α∞tables␈α
analogously␈α
to␈α∞the␈α
decidability␈α∞of␈α
propositional
␈↓ ↓H␈↓sentences.


␈↓ ↓H␈↓αLambda-expressions.

␈↓ ↓H␈↓        The␈α∪only␈α∩rule␈α∪required␈α∩for␈α∪handling␈α∪lambda-expressions␈α∩in␈α∪first␈α∩order␈α∪logic␈α∪is␈α∩called
␈↓ ↓H␈↓␈↓↓lambda-conversion.␈↓  Essentially it is

␈↓ ↓H␈↓␈↓ βz␈↓↓[λ␈↓πx␈↓↓: e][t] =␈↓ < the result of substituting ␈↓↓t␈↓ for ␈↓πx␈↓ in ␈↓↓e␈↓ >. 

␈↓ ↓H␈↓As examples of this rule, we have

␈↓ ↓H␈↓␈↓ ∧+␈↓↓[λx: ␈↓αa|␈↓↓x . y][u . v] = [␈↓αa|␈↓↓[u . v]] . y .           ␈↓ 

␈↓ ↓H␈↓However,␈αa␈α
complication␈αrequires␈α
modifying␈αthe␈αrule.␈α
 Namely,␈αwe␈α
can't␈αsubstitute␈α
for␈αa␈αvariable␈α
␈↓πx␈↓
␈↓ ↓H␈↓an␈α
expression␈α␈↓↓e␈↓␈α
that␈α
has␈αa␈α
free␈α
variable␈α␈↓πx␈↓␈↓β1␈↓␈α
into␈α
a␈αcontext␈α
in␈α
which␈α␈↓πx␈↓␈↓β1␈↓␈α
is␈α
bound.␈α Thus␈α
it␈αwould␈α
be
␈↓ ↓H␈↓wrong␈αto␈αsubstitute␈α␈↓↓␈↓πx␈↓↓␈↓β1␈↓↓+ ␈↓πx␈↓↓␈↓β2␈↓↓␈↓␈αfor␈α␈↓πx␈↓␈αin␈α␈↓↓∀␈↓πx␈↓↓␈↓β1␈↓↓: [␈↓πx␈↓↓ + ␈↓πx␈↓↓␈↓β1␈↓↓ = ␈↓πx␈↓↓␈↓β2␈↓↓]␈↓␈αor␈αinto␈αthe␈αterm␈α␈↓↓[λ␈↓πx␈↓↓␈↓β1␈↓↓: ␈↓πx␈↓↓ + ␈↓πx␈↓↓␈↓β1␈↓↓][u + v]␈↓.␈α Before
␈↓ ↓H␈↓doing␈α
the␈α
substitution,␈αthe␈α
variable␈α
␈↓πx␈↓␈↓β1␈↓␈αwould␈α
have␈α
to␈α
be␈αreplaced␈α
in␈α
all␈αits␈α
bound␈α
occurrences␈αby␈α
a
␈↓ ↓H␈↓new variable.

␈↓ ↓H␈↓        Lambda-expressions␈α∀can␈α∀always␈α∃be␈α∀eliminated␈α∀from␈α∃sentences␈α∀and␈α∀terms␈α∃by␈α∀lambda-
␈↓ ↓H␈↓conversion,␈αbut␈αthe␈αexpression␈αmay␈α
increase␈αgreatly␈αin␈αlength␈αif␈α
a␈αlengthy␈αterm␈αreplaces␈αa␈α
variable
␈↓ ↓H␈↓that␈α∞occurs␈α∞more␈α∞than␈α∞once␈α∞in␈α
␈↓↓e.␈↓␈α∞ It␈α∞is␈α∞easy␈α∞to␈α∞make␈α
an␈α∞expression␈α∞of␈α∞length␈α∞proportional␈α∞to␈α
␈↓↓n␈↓
␈↓ ↓H␈↓whose␈α⊃length␈α⊃is␈α⊃proportional␈α∩to␈α⊃2␈↓∧n␈↓␈α⊃after␈α⊃conversion␈α∩of␈α⊃its␈α⊃␈↓↓n␈↓␈α⊃nested␈α∩lambda-expressions.␈α⊃ For
␈↓ ↓H␈↓example         ␈↓ ∧X␈↓↓λ␈↓πx␈↓↓␈↓β1␈↓↓: [␈↓πx␈↓↓␈↓β1␈↓↓.␈↓πx␈↓↓␈↓β1␈↓↓][ ... [λ␈↓πx␈↓↓␈↓βn␈↓↓: [␈↓πx␈↓↓␈↓βn␈↓↓.␈↓πx␈↓↓␈↓βn␈↓↓][␈↓¬A␈↓↓]] ... ]␈↓ 
␈↓ ↓H␈↓62␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓becomes

␈↓ ↓H␈↓                ␈↓¬(A . A)␈↓,

␈↓ ↓H␈↓                ␈↓¬((A . A) . (A . A))␈↓,

␈↓ ↓H␈↓or

␈↓ ↓H␈↓                ␈↓¬((((A . A) . (A . A)) . ((A . A) . (A . A))) ␈↓
␈↓ ↓H␈↓                ␈↓¬  . ((A . A) . (A . A)) . ((A . A) . (A . A))))␈↓

␈↓ ↓H␈↓for ␈↓↓n␈↓ = 1, 2, or 4 respectively.

␈↓ ↓H␈↓        The␈α∂use␈α∞of␈α∂λ-expressions␈α∞in␈α∂writing␈α∂programs␈α∞also␈α∂may␈α∞improve␈α∂efficiency␈α∂by␈α∞specifying
␈↓ ↓H␈↓some␈α∞the␈α∂expression␈α∞be␈α∞evaluated␈α∂once␈α∞and␈α∞the␈α∂value␈α∞used␈α∞in␈α∂several␈α∞places.␈α∞ Where␈α∂there␈α∞are
␈↓ ↓H␈↓side-effects, of course more than efficiency is effected.



␈↓ ↓H␈↓4.  ␈↓αLists, natural numbers and LISP program primitives.␈↓


␈↓ ↓H␈↓        We␈α
now␈α
extend␈α
the␈α
theory␈α
of␈α
S-expressions␈α∞in␈α
order␈α
to␈α
talk␈α
about␈α
the␈α
subdomain␈α∞of␈α
lists
␈↓ ↓H␈↓and␈αnatural␈αnumbers.␈α  We␈αalso␈αdefine␈αfunctions␈αcorresponding␈αto␈αthe␈αbasic␈αLISP␈αprograms␈αother
␈↓ ↓H␈↓that ␈↓↓car,␈↓ ␈↓↓cdr␈↓ and ␈↓↓cons␈↓ that were described in Chapter I.


␈↓ ↓H␈↓αThe Theory of lists.

␈↓ ↓H␈↓        The␈α∞domain␈α∞of␈α
lists␈α∞is␈α∞a␈α
subdomain␈α∞of␈α∞the␈α
domain␈α∞of␈α∞S-expressions␈α
which␈α∞we␈α∞denote␈α
by
␈↓ ↓H␈↓␈↓↓LIST.␈↓␈α∂ It␈α∂is␈α∂the␈α∂union␈α∞of␈α∂the␈α∂one␈α∂element␈α∂domain␈α∞␈↓↓NULL␈↓␈α∂and␈α∂the␈α∂domain␈α∂of␈α∂non-empty␈α∞lists
␈↓ ↓H␈↓␈↓↓NELIST.␈↓  These domain facts are given by

␈↓ ↓H␈↓␈↓ βx␈↓↓LIST ⊂ SEXP␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓NELIST ⊂ PAIR␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓NULL ≡ {␈↓¬NIL␈↓↓}␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓LIST = NULL ∪ LIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓NULL ∩ LIST = ␈↓πf␈↓↓␈↓

␈↓ ↓H␈↓The variables ␈↓↓u, v, w␈↓ will range over lists and ␈↓↓uu, vv, ww␈↓ will range over non-empty lists.

␈↓ ↓H␈↓␈↓ βx␈↓↓u, v, w ε LIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓uu, vv, ww ε NELIST␈↓

␈↓ ↓H␈↓        The␈α∪behaviour␈α∪of␈α∪␈↓↓car,␈↓␈α∪␈↓↓cdr␈↓␈α∪and␈α∪␈↓↓cons␈↓␈α∩on␈α∪the␈α∪domain␈α∪␈↓↓LIST␈↓␈α∪is␈α∪given␈α∪by␈α∪the␈α∩function
␈↓ ↓H␈↓mappings:

␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αa␈↓↓: NELIST → SEXP␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αd␈↓↓: NELIST → LIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αcons␈↓↓: SEXP ␈↓¬x␈↓↓ LIST → NELIST␈↓
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *63


␈↓ ↓H␈↓        Since␈α∂lists␈α⊂are␈α∂a␈α⊂subdomain␈α∂of␈α⊂S-expressions␈α∂and␈α⊂non-empty␈α∂lists␈α⊂a␈α∂subdomain␈α⊂of␈α∂non-
␈↓ ↓H␈↓atomic␈α
S-expressions␈α
the␈α
axioms␈α∞␈↓¬CAR,␈α
␈↓␈↓¬CDR␈α
␈↓and␈α
␈↓¬CONS␈α∞␈↓␈α
apply␈α
to␈α
lists␈α∞also.␈α
 For␈α
lists␈α
we␈α∞have␈α
the
␈↓ ↓H␈↓following structural induction principle.

␈↓ ↓H␈↓␈↓¬LISTINDUCTION: ␈↓␈↓ ∧8␈↓↓␈↓πF␈↓↓ ␈↓¬NIL␈↓↓ ∧ ∀uu: [␈↓πF␈↓↓ ␈↓αd|␈↓↓uu ⊃ ␈↓πF␈↓↓ uu] ⊃ ∀u: ␈↓πF␈↓↓ u ␈↓. 


␈↓ ↓H␈↓αNatural numbers.

␈↓ ↓H␈↓        We␈α
will␈α
treat␈α
the␈α
domain␈α
of␈α
natural␈α
numbers␈α
as␈α
a␈α
subdomain␈α
of␈α
the␈α
atoms.␈α∞ This␈α
reflects
␈↓ ↓H␈↓the␈αway␈α
LISP␈αsystems␈α
treat␈αnumbers␈α
and␈αmakes␈α
it␈αpossible␈α
to␈αdiscuss␈α
mixed␈αsymbolic␈αand␈α
numeric
␈↓ ↓H␈↓expressions␈αconveniently.␈α The␈α
natural␈αnumbers␈αwill␈α
be␈αdenoted␈αby␈α
␈↓↓NATNUM␈↓␈αand␈αis␈α
the␈αunion
␈↓ ↓H␈↓of the one element domain ␈↓↓ZERO␈↓ and the domain of positive numbers ␈↓↓POSP.␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓NATNUM ⊂ ATOM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ZERO ≡ {␈↓¬0␈↓↓}␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓NATNUM = ZER0 ∪ POSP␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ZERO ∩ POSP = ␈↓πf␈↓↓␈↓

␈↓ ↓H␈↓        We␈α
will␈α
use␈α
the␈α
ordinary␈α
numerals␈α
␈↓¬0,␈α
␈↓␈↓¬1,␈α
␈↓␈↓¬2,␈α
␈↓etc.␈α
to␈α
denote␈α
numbers.␈α
The␈α
variables␈α∞␈↓↓l,␈α
n,
␈↓ ↓H␈↓↓m␈↓ will range over numbers and ␈↓↓ll, mm, nn␈↓ will range over positive numbers.

␈↓ ↓H␈↓␈↓ βx␈↓↓l, n, m ε NATNUM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ll, mm, nn ε POSP␈↓

␈↓ ↓H␈↓        The␈α
basic␈αfunctions␈α
on␈α
numbers␈αare␈α
␈↓↓successor␈↓␈α
and␈α␈↓↓predecessor␈↓␈α
which␈α
we␈αwill␈α
denote␈αby␈α
␈↓↓add1␈↓
␈↓ ↓H␈↓and␈α␈↓↓sub1␈↓␈α
corresponding␈αto␈αnames␈α
of␈αthe␈αLISP␈α
programs␈αthat␈αcompute␈α
these␈αfunctions.␈α ␈↓↓add1␈↓␈α
maps
␈↓ ↓H␈↓numbers to wo positive numbers and ␈↓↓sub1␈↓ maps positive numbers to numbers.

␈↓ ↓H␈↓␈↓ βx␈↓↓add1: NATNUM → POSP␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓sub1: POSP → NATNUM␈↓

␈↓ ↓H␈↓␈↓ βx␈↓¬SUB1:␈↓  ␈↓↓∀n: sub1 add1 n = n␈↓
␈↓ ↓H␈↓␈↓ βx␈↓¬ADD1:␈↓  ␈↓↓∀nn: add1 sub1 nn = nn␈↓

␈↓ ↓H␈↓Our form of the induction principle for natural numbers is:

␈↓ ↓H␈↓␈↓¬NUMINDUCTION: ␈↓␈↓ ∧0␈↓↓␈↓πF␈↓↓ ␈↓¬0 ␈↓↓∧ ∀nn: [␈↓πF␈↓↓ sub1 nn ⊃ ␈↓πF␈↓↓ nn] ⊃ ∀n: ␈↓πF␈↓↓ n.␈↓ 

␈↓ ↓H␈↓        This␈α∂ends␈α∂the␈α∞description␈α∂of␈α∂the␈α∞basic␈α∂theory␈α∂of␈α∞S-expressions,␈α∂lists␈α∂and␈α∂numbers.␈α∞  Our
␈↓ ↓H␈↓theory␈αis␈α
a␈αdynamic␈αone,␈α
however,␈αand␈α
we␈αwill␈αintroduce␈α
new␈αfunction␈α
and␈αpredicate␈αsymbols␈α
with
␈↓ ↓H␈↓defining axioms as we proceed.

␈↓ ↓H␈↓[Remark:␈α∞ there␈α∂are␈α∞domains␈α∂other␈α∞than␈α∂S-expressions␈α∞where␈α∂the␈α∞facts␈α∂we␈α∞have␈α∂specified␈α∞hold.
␈↓ ↓H␈↓For␈α⊂example␈α⊃we␈α⊂have␈α⊃not␈α⊂specified␈α⊃the␈α⊂value␈α⊃of␈α⊂␈↓↓car␈↓␈α⊃and␈α⊂␈↓↓cdr␈↓␈α⊃on␈α⊂atoms,␈α⊃and␈α⊂there␈α⊃are␈α⊂many
␈↓ ↓H␈↓consistent␈α
possiblilities.␈α
 The␈α
additional␈αfacts␈α
we␈α
can␈α
prove␈α
using␈αany␈α
valid␈α
proof␈α
rules␈α
will␈αalso␈α
be
␈↓ ↓H␈↓true in a such domain.]
␈↓ ↓H␈↓64␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓αAxioms for basic LISP programs.

␈↓ ↓H␈↓        As␈α⊂advertised,␈α⊃we␈α⊂are␈α⊂going␈α⊃to␈α⊂extend␈α⊂the␈α⊃basic␈α⊂theory␈α⊂by␈α⊃defining␈α⊂new␈α⊃functions␈α⊂and
␈↓ ↓H␈↓predicates.␈α In␈α
order␈αto␈αrepresent␈α
LISP␈αprograms␈αas␈α
functions␈αin␈α
a␈αfirst␈αorder␈α
theory,␈αwe␈αfirst␈α
need
␈↓ ↓H␈↓to␈αrepresent␈αthe␈αbasic␈αprograms.␈α We␈αhave␈αalready␈αintroduced␈α␈↓↓car,␈↓␈α␈↓↓cdr␈↓␈αand␈α␈↓↓cons.␈↓␈α For␈αthe␈αrest␈αwe
␈↓ ↓H␈↓add the ternary function symbol ␈↓αif␈↓, to represent the conditional.  We will generally write

␈↓ ↓H␈↓␈↓ ∧.␈↓↓␈↓αif␈↓↓ t␈↓β0␈↓↓ ␈↓αthen␈↓↓ t␈↓β1␈↓↓ ␈↓αelse␈↓↓ t␈↓β2␈↓↓ ␈↓ instead of ␈↓↓ ␈↓αif␈↓↓[t␈↓β0␈↓↓,t␈↓β1␈↓↓,t␈↓β2␈↓↓]␈↓ 

␈↓ ↓H␈↓as␈α
in␈αthe␈α
external␈α
notation␈αfor␈α
programs.␈α
 We␈αalso␈α
add␈α
the␈αfunction␈α
symbols␈α
␈↓αequal␈↓,␈α␈↓αat␈↓,␈α
␈↓αn␈↓,␈α␈↓αnot␈↓,␈α
␈↓αand␈↓,
␈↓ ↓H␈↓␈↓αor␈↓ corresponding to external notation.  The axioms characterizing these functions are:

␈↓ ↓H␈↓␈↓¬IF: ␈↓␈↓ α8␈↓↓∀x y z: ␈↓αif␈↓↓ x ␈↓αthen␈↓↓ y ␈↓αelse␈↓↓ z] = IF x ≠ ␈↓¬NIL␈↓↓ THEN y ELSE z␈↓
␈↓ ↓H␈↓␈↓¬EQUAL: ␈↓␈↓ α8␈↓↓∀x y: [x ␈↓αequal␈↓↓ y] = IF [x = y] THEN ␈↓¬T␈↓↓ ELSE ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓¬ATOM: ␈↓␈↓ α8␈↓↓∀x: ␈↓αat|␈↓↓x = IF ATOM x THEN ␈↓¬T␈↓↓ ELSE ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓¬NULL: ␈↓␈↓ α8␈↓↓∀X: ␈↓αn|␈↓↓x = IF NULL x THEN ␈↓¬T␈↓↓ ELSE ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓¬NOT: ␈↓␈↓ α8␈↓↓∀x: ␈↓αnot␈↓↓ x = ␈↓αif␈↓↓ x ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓¬T␈↓↓␈↓
␈↓ ↓H␈↓␈↓¬AND: ␈↓␈↓ α8␈↓↓∀x y: x and y = ␈↓αif␈↓↓ x ␈↓αthen␈↓↓ y ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓¬OR: ␈↓␈↓ α8␈↓↓∀x y: x or y = ␈↓αif␈↓↓ x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ y␈↓

␈↓ ↓H␈↓        For␈α⊃purposes␈α∩of␈α⊃talking␈α∩about␈α⊃LISP␈α∩programs␈α⊃we␈α⊃define␈α∩the␈α⊃subclass␈α∩of␈α⊃terms␈α∩in␈α⊃our
␈↓ ↓H␈↓language␈αknown␈αas␈αLISP␈αterm.␈α Variables␈αand␈α
S-expression␈αconstants␈αare␈αLISP␈αterms.␈α If␈α␈↓↓f␈↓␈αis␈α
one
␈↓ ↓H␈↓of␈α∞the␈α∞basic␈α
LISP␈α∞functions␈α∞␈↓αa␈↓,␈α
␈↓αd␈↓,␈α∞␈↓αcons␈↓,␈α∞␈↓αequal␈↓,␈α∞␈↓αat␈↓,␈α
␈↓αn␈↓,␈α∞␈↓αnot␈↓,␈α∞␈↓αand␈↓,␈α
or␈α∞␈↓αor␈↓,␈α∞or␈α
the␈α∞name␈α∞of␈α∞a␈α
function
␈↓ ↓H␈↓representing␈α∀a␈α∀LISP␈α∀program,␈α∀or␈α∃a␈α∀"new"␈α∀function␈α∀symbol␈α∀and␈α∃the␈α∀arity␈α∀of␈α∀␈↓↓f␈↓␈α∀is␈α∃␈↓↓n,␈↓␈α∀and
␈↓ ↓H␈↓␈↓↓t␈↓β0␈↓↓, t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓␈↓␈αare␈αLISP␈αterms␈αthen␈α␈↓↓f[t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓]␈↓␈αand␈α␈↓↓[λx␈↓β1␈↓↓ ... x␈↓βn␈↓↓: t␈↓β0␈↓↓][t␈↓β1␈↓↓, ... ,t␈↓βn␈↓↓]␈↓␈αare␈αLISP␈αterms.
␈↓ ↓H␈↓And␈α∞those␈α
are␈α∞all␈α
the␈α∞LISP␈α
terms.␈α∞ Notice␈α∞that␈α
these␈α∞correspond␈α
to␈α∞the␈α
terms␈α∞of␈α∞LISP␈α
program
␈↓ ↓H␈↓definitions omitting the label construct and programs as arguments.

␈↓ ↓H␈↓The␈α∩restriction␈α∩to␈α∩S-expression␈α∪constants␈α∩may␈α∩seem␈α∩vacuous␈α∩now,␈α∪as␈α∩those␈α∩are␈α∩all␈α∪we␈α∩have
␈↓ ↓H␈↓mentioned so far, but but there will be more.



␈↓ ↓H␈↓5.  ␈↓αRepresenting programs known to terminate.␈↓


␈↓ ↓H␈↓        Now␈α⊂we␈α⊂are␈α⊂ready␈α∂to␈α⊂explain␈α⊂how␈α⊂(some)␈α⊂LISP␈α∂programs␈α⊂can␈α⊂be␈α⊂represented.␈α⊂ First␈α∂we
␈↓ ↓H␈↓consider␈α
only␈α
programs␈α
known␈α
to␈α
terminate.␈α
 There␈α
are␈α
several␈α
syntactic␈α
criteria␈α
defining␈α
classes␈α
of
␈↓ ↓H␈↓such␈αprograms.␈α They␈αare␈αsaid␈αto␈αterminate␈αon␈αform.␈α We␈αwill␈αconsider␈αthree␈αforms:␈αlist␈αrecursion,
␈↓ ↓H␈↓S-expression␈αrecursion␈αand␈αnumeric␈αrecursion.␈α  They␈αinclude␈αa␈αlarge␈αnumber␈αof␈αuseful␈αprograms
␈↓ ↓H␈↓and␈α
the␈α
forms␈α
are␈α
easy␈α∞to␈α
recognize.␈α
 We␈α
give␈α
some␈α∞simple␈α
examples␈α
to␈α
illustrate␈α
the␈α∞basic␈α
style
␈↓ ↓H␈↓and␈α
method␈αof␈α
proof.␈α
 Later␈αwe␈α
will␈αshow␈α
how␈α
to␈αrepresent␈α
programs␈α
not␈αknown␈α
to␈αterminate,␈α
and
␈↓ ↓H␈↓how␈α
to␈α
prove␈α
termination␈α
in␈α
some␈α
cases.␈α
 We␈αwill␈α
be␈α
able␈α
to␈α
prove␈α
termination␈α
of␈α
the␈α
classes␈αof
␈↓ ↓H␈↓programs␈α
discussed␈α
in␈α
this␈α
section,␈α
so␈α
this␈α
will␈α
turn␈α
out␈α
to␈α
be␈α
a␈α
special␈α
case␈α
of␈α
the␈α
more␈αgeneral
␈↓ ↓H␈↓method.␈α⊂ We␈α⊂start␈α∂with␈α⊂the␈α⊂special␈α∂case␈α⊂as␈α⊂it␈α∂is␈α⊂easier␈α⊂to␈α∂explain␈α⊂and␈α⊂allows␈α∂us␈α⊂to␈α⊂get␈α⊂to␈α∂the
␈↓ ↓H␈↓proving part with less fuss.

␈↓ ↓H␈↓        Consider a LISP program given by a definition of the form

␈↓ ↓H␈↓5.1)␈↓ ¬[␈↓↓f[x]←␈↓πt␈↓↓[f,x]       ␈↓ 
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *65


␈↓ ↓H␈↓where␈α⊂␈↓πt␈↓␈α⊂is␈α⊂a␈α⊂LISP␈α⊂term␈α⊂involving␈α⊂the␈α∂variable␈α⊂␈↓↓x,␈↓␈α⊂the␈α⊂program␈α⊂name␈α⊂␈↓↓f␈↓␈α⊂and␈α⊂the␈α⊂basic␈α∂LISP
␈↓ ↓H␈↓programs␈αS-expression␈α
constants␈αand␈α
perhaps␈αnames␈α
of␈αpreviously␈α
defined␈αprograms.␈α
  (See␈α␈↓π∞␈↓4␈α
for
␈↓ ↓H␈↓more␈αcomplete␈αdefinition␈α
of␈αLISP␈αterm.)  ␈αThis␈α
program␈αpartially␈αdefines␈α
a␈αfunction␈αalso␈αcalled␈α
␈↓↓f,␈↓
␈↓ ↓H␈↓on␈α∞S-expressions␈α∂such␈α∞that␈α∞whenever␈α∂␈↓↓x␈↓␈α∞is␈α∞such␈α∂that␈α∞the␈α∞program␈α∂terminates␈α∞with␈α∞result␈α∂␈↓↓y␈↓␈α∞then
␈↓ ↓H␈↓␈↓↓f x=y␈↓.␈α
 Using␈α∞the␈α
rules␈α∞for␈α
computation␈α∞given␈α
in␈α∞Chapter␈α
I␈α
we␈α∞can␈α
see␈α∞that␈α
for␈α∞␈↓↓x␈↓␈α
such␈α∞that␈α
the
␈↓ ↓H␈↓computation␈α⊃of␈α⊃␈↓↓f[x]␈↓␈α⊂terminates␈α⊃we␈α⊃have␈α⊃␈↓↓f[x] = ␈↓πt␈↓↓[f,x].␈↓␈α⊂ Due␈α⊃to␈α⊃the␈α⊂double␈α⊃use␈α⊃of␈α⊃symbols␈α⊂as
␈↓ ↓H␈↓external␈α
notation␈αfor␈α
programs␈αand␈α
to␈αdenote␈α
functions␈αin␈α
our␈αtheory,␈α
and␈αthe␈α
manner␈α
in␈αwhich
␈↓ ↓H␈↓the␈α
basic␈α
LISP␈α
function␈α
were␈α
axiomatized,␈α
this␈α
equation␈α
holds␈α
whether␈α
it␈α
is␈α
viewed␈α
as␈α
an␈α
equation
␈↓ ↓H␈↓between␈α∞program␈α∂terms␈α∞or␈α∞as␈α∂an␈α∞equation␈α∂in␈α∞the␈α∞logic␈α∂(for␈α∞those␈α∞␈↓↓x␈↓␈α∂such␈α∞ that␈α∂the␈α∞computation
␈↓ ↓H␈↓halts).␈α Thus,␈αgiven␈αa␈α
domain␈αon␈αwhich␈αthe␈αprogram␈α
␈↓↓f␈↓␈αis␈αknown␈αto␈α
terminate␈αthere␈αis␈αa␈αfunction␈α
␈↓↓f␈↓
␈↓ ↓H␈↓completely␈α∞characterized␈α∞on␈α
that␈α∞domain␈α∞by␈α
the␈α∞functional␈α∞equation␈α
corresponding␈α∞to␈α∞(5.1).␈α
 We
␈↓ ↓H␈↓can␈αadd␈αa␈αnew␈αfunction␈αsymbol␈αto␈αthe␈αlanguage␈αnaming␈αthis␈αfunction␈αand␈αthe␈αaxiom␈αobtain␈αfrom
␈↓ ↓H␈↓(5.1)␈α
by␈α∞replacing␈α
"←"␈α
by␈α∞"=",␈α
replacing␈α
the␈α∞function␈α
parameter␈α
by␈α∞the␈α
new␈α
function␈α∞symbol␈α
and
␈↓ ↓H␈↓universally␈α
quantifying␈α
over␈α
␈↓↓x.␈↓␈α
 Clearly␈α
this␈α∞reasoning␈α
applies␈α
to␈α
programs␈α
with␈α
more␈α∞than␈α
one
␈↓ ↓H␈↓input parameter.

␈↓ ↓H␈↓        In␈αChapter␈αII␈αwe␈αdiscussed␈αvarious␈αforms␈α
of␈αrecursive␈αdefinitions.␈α  Some␈αof␈αthese␈αforms␈α
we
␈↓ ↓H␈↓claimed␈α∞had␈α∞the␈α∞property␈α∂that␈α∞any␈α∞program␈α∞having␈α∞that␈α∂form␈α∞terminates␈α∞for␈α∞all␈α∂allowed␈α∞input.
␈↓ ↓H␈↓One such form was list recursion:

␈↓ ↓H␈↓5.2)␈↓ β≡␈↓↓f[u,␈↓πx␈↓↓] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ g[␈↓πx␈↓↓] ␈↓αelse␈↓↓ h[␈↓αa|␈↓↓u, ␈↓αd|␈↓↓u, ␈↓πx␈↓↓, f[␈↓αd|␈↓↓u, j[␈↓αa|␈↓↓u, ␈↓αd|␈↓↓u, ␈↓πx␈↓↓]]␈↓. 

␈↓ ↓H␈↓The␈α
symbols␈α␈↓↓g,␈↓␈α
␈↓↓h␈↓␈αand␈α
␈↓↓j␈↓␈α occuring␈α
in␈α
these␈αdefinitions␈α
must␈αdenote␈α
programs␈αdefinable␈α
in␈αterms␈α
of
␈↓ ↓H␈↓previously␈α⊂defined␈α⊂programs␈α∂(or␈α⊂basic␈α⊂programs),␈α∂must␈α⊂be␈α⊂known␈α∂to␈α⊂terminate␈α⊂on␈α∂appropriate
␈↓ ↓H␈↓domains␈αwith␈αappropriate␈αranges.␈α In␈αparticular␈αif␈α
 ␈↓↓g␈↓␈αterminates␈αon␈αinput␈αin␈αdomain␈α␈↓↓D␈↓β0␈↓↓␈↓␈α
returning
␈↓ ↓H␈↓results␈α∪in␈α∀␈↓↓D␈↓β1␈↓↓␈↓,␈α∪␈↓↓j␈↓␈α∀terminates␈α∪on␈α∀input␈α∪in␈α∪␈↓↓SEXP ␈↓¬x␈↓↓ LIST ␈↓¬x␈↓↓ D␈↓β0␈↓↓␈↓␈α∀returning␈α∪results␈α∀in␈α∪␈↓↓D␈↓β0␈↓↓␈↓␈α∀and␈α∪␈↓↓h␈↓
␈↓ ↓H␈↓terminates␈α
on␈α
input␈αin␈α
␈↓↓SEXP ␈↓¬x␈↓↓ LIST ␈↓¬x␈↓↓ D␈↓β0␈↓↓ ␈↓¬x␈↓↓ D␈↓β1␈↓↓∪D␈↓β2␈↓↓␈↓␈α
returning␈αresults␈α
in␈α
␈↓↓D␈↓β2␈↓↓␈↓␈αthen␈α
the␈α
program␈α␈↓↓f␈↓
␈↓ ↓H␈↓will␈α
terminate␈αreturning␈α
results␈α
in␈α␈↓↓D␈↓β1␈↓↓␈↓␈α
on␈αarguments␈α
in␈α
␈↓↓NULL ␈↓¬x␈↓↓ D␈↓β0␈↓↓␈↓␈αand␈α
in␈α␈↓↓D␈↓β2␈↓↓␈↓␈α
for␈α
arguments␈αin
␈↓ ↓H␈↓␈↓↓NELIST ␈↓¬x␈↓↓ D␈↓β0␈↓↓␈↓.

␈↓ ↓H␈↓[Remark:␈α
For␈αsimplicity␈α
we␈α
have␈αincluded␈α
just␈α
a␈αsingle␈α
parameter␈α␈↓πx␈↓␈α
in␈α
each␈αof␈α
the␈α
above␈αforms.
␈↓ ↓H␈↓In␈α⊂general␈α⊂we␈α⊂allow␈α⊂any␈α⊂number␈α⊂of␈α⊂parameters␈α⊂(including␈α⊂none)␈α⊂to␈α⊂appear,␈α⊂and␈α⊂they␈α⊂may␈α∂be
␈↓ ↓H␈↓restricted to whatever domains we know about.]

␈↓ ↓H␈↓For␈αsuch␈αa␈α
program,␈αif␈αthe␈αprograms␈α
␈↓↓g,␈↓␈α␈↓↓h,␈↓␈α␈↓↓j␈↓␈αare␈α
represented␈αby␈αfunctions␈α␈↓↓gg,␈↓␈α
␈↓↓hh,␈↓␈α␈↓↓jj␈↓␈αthen␈αwe␈α
will
␈↓ ↓H␈↓have the domain mappings:

␈↓ ↓H␈↓␈↓ βx␈↓↓gg: D␈↓β0␈↓↓ → D␈↓β1␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓jj: SEXP ␈↓¬x␈↓↓ LIST ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β0␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓hh: SEXP ␈↓¬x␈↓↓ LIST ␈↓¬x␈↓↓ D␈↓β0␈↓↓ ␈↓¬x␈↓↓ D → D␈↓β2␈↓↓␈↓

␈↓ ↓H␈↓and if ␈↓↓ff␈↓ is the function representing ␈↓↓f␈↓ we will add the domain mappings

␈↓ ↓H␈↓␈↓ βx␈↓↓ff: LIST ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β1␈↓↓ ∪ D␈↓β2␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ff: NULL ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β1␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ff: NELIST ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β2␈↓↓␈↓

␈↓ ↓H␈↓and the axiom characterizing ␈↓↓ff␈↓ will be:

␈↓ ↓H␈↓␈↓ αx␈↓↓∀u ␈↓πx␈↓↓: ff[u,␈↓πx␈↓↓]=␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ gg[␈↓πx␈↓↓] ␈↓αelse␈↓↓ hh[␈↓αa|␈↓↓u,␈↓αd|␈↓↓u,␈↓πx␈↓↓,  ff[␈↓αd|␈↓↓u,jj[␈↓αa|␈↓↓u,␈↓αd|␈↓↓u,␈↓πx␈↓↓]]␈↓.
␈↓ ↓H␈↓66␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓where ␈↓πx␈↓ ranges over ␈↓↓D␈↓β0␈↓↓␈↓.

␈↓ ↓H␈↓        The progam for ␈↓↓append␈↓ has the form of a list recursion.

␈↓ ↓H␈↓␈↓¬APPEND: ␈↓  ␈↓↓u*v ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]␈↓.

␈↓ ↓H␈↓Namely,␈α∪␈↓πx␈↓␈α∩is␈α∪␈↓↓v,␈↓␈α∪␈↓↓D␈↓β0␈↓↓,D␈↓β1␈↓↓,D␈↓β2␈↓↓␈↓␈α∩are␈α∪␈↓↓LIST,␈↓␈α∪␈↓↓g␈↓␈α∩is␈α∪the␈α∩identity,␈α∪␈↓↓j␈↓␈α∪returns␈α∩its␈α∪third␈α∪argument␈α∩and
␈↓ ↓H␈↓␈↓↓h[x,w,v,u]␈↓␈αis␈α␈↓↓x . u␈↓.␈α
 which␈αhas␈αrange␈α␈↓↓NELIST.␈↓␈α
 Note␈αthat␈αif␈α
␈↓↓v␈↓␈αis␈αreplace␈αby␈α
␈↓↓vv␈↓␈αthen␈αall␈α
of␈αthe
␈↓ ↓H␈↓component␈α
programs␈α
return␈α
values␈α
in␈α
␈↓↓NELIST.␈↓␈α
 We␈α
can␈α
thus␈α
introduce␈α
the␈α
function␈α
also␈α
denoted
␈↓ ↓H␈↓by infix * by giving the domain mappings and equational axiom as follows:

␈↓ ↓H␈↓␈↓ βx␈↓↓*: LIST ␈↓¬x␈↓↓ LIST → LIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓*: NULL ␈↓¬x␈↓↓ LIST → LIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓*: NELIST ␈↓¬x␈↓↓ LIST → NELIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓*: LIST ␈↓¬x␈↓↓ NELIST → NELIST␈↓

␈↓ ↓H␈↓and the axiom characterizing ␈↓↓ff␈↓ will be:

␈↓ ↓H␈↓␈↓¬APPEND-DEF: ␈↓  ␈↓↓∀u v: u*v = ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]␈↓.

␈↓ ↓H␈↓Two immediate consequences of the defining axiom are:

␈↓ ↓H␈↓␈↓¬APPEND-NIL: ␈↓  ␈↓↓∀v: ␈↓¬NIL␈↓↓*v = v,     ␈↓
␈↓ ↓H␈↓␈↓¬APPEND-UU: ␈↓  ␈↓↓∀uu v: uu*v = ␈↓αa|␈↓↓uu . [␈↓αd|␈↓↓uu *v],     ␈↓

␈↓ ↓H␈↓To␈αprove␈α␈↓¬APPEND-NIL␈α␈↓we␈αsubstitute␈α␈↓¬NIL␈↓␈αfor␈α␈↓↓u␈↓␈αin␈α␈↓¬APPEND-DEF␈α␈↓and␈αuse␈αthe␈αaxioms␈α␈↓¬NULL␈α␈↓and␈α␈↓¬IF.
␈↓ ↓H␈↓¬␈↓ Thus

␈↓ ↓H␈↓␈↓ αX␈↓↓␈↓¬NIL␈↓↓*v=␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓¬NIL␈↓↓ ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬NIL␈↓↓ . [␈↓αd|␈↓↓␈↓¬NIL␈↓↓ * v]␈↓␈↓ πX; ␈↓¬APPEND-DEF ␈↓
␈↓ ↓H␈↓␈↓ αX␈↓↓      =␈↓αif␈↓↓ ␈↓¬T␈↓↓ ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓␈↓¬NIL␈↓↓ . [␈↓αd|␈↓↓␈↓¬NIL␈↓↓ * v]␈↓␈↓ πX; ␈↓¬NULL ␈↓
␈↓ ↓H␈↓␈↓ αX␈↓↓      = v ␈↓␈↓ πX; ␈↓¬IF ␈↓

␈↓ ↓H␈↓The␈α∂proof␈α∞of␈α∂␈↓¬APPEND-UU␈α∂␈↓is␈α∞similar.␈α∂ Here␈α∂we␈α∞need␈α∂to␈α∂know␈α∞that␈α∂␈↓↓∀uu:␈α∂¬NULL␈α∞uu␈↓␈α∂which␈α∂is␈α∞a
␈↓ ↓H␈↓consequence of the domain facts ␈↓↓uu ε NELIST␈↓ and ␈↓↓NULL ∩ NELIST = qf␈↓.

␈↓ ↓H␈↓␈↓ αX␈↓↓uu*v=␈↓αif␈↓↓ ␈↓αn|␈↓↓uu ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓uu . [␈↓αd|␈↓↓uu * v]␈↓␈↓ πX; ␈↓¬APPEND-DEF ␈↓
␈↓ ↓H␈↓␈↓ αX␈↓↓      =␈↓αif␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓uu . [␈↓αd|␈↓↓uu * v]␈↓␈↓ πX; ␈↓¬NULL ␈↓and domain facts
␈↓ ↓H␈↓␈↓ αX␈↓↓      = ␈↓αa|␈↓↓uu . [␈↓αd|␈↓↓uu * v] ␈↓␈↓ πX; ␈↓¬IF ␈↓

␈↓ ↓H␈↓In␈α
what␈αfollows␈α
we␈α
will␈αnot␈α
continue␈αto␈α
give␈α
proofs␈αof␈α
such␈αelementary␈α
facts,␈α
but␈αjust␈α
state␈αthem␈α
as
␈↓ ↓H␈↓immediate corollaries of the defining axiom.

␈↓ ↓H␈↓        The␈α
main␈α
reason␈α
for␈α
restricting␈αthe␈α
second␈α
argument␈α
of␈α
␈↓↓append␈↓␈αto␈α
␈↓↓LIST␈↓␈α
is␈α
that␈α
the␈αintent␈α
is
␈↓ ↓H␈↓to␈α⊃stick␈α∩two␈α⊃lists␈α∩together␈α⊃to␈α⊃get␈α∩a␈α⊃new␈α∩one.␈α⊃ Also,␈α⊃when␈α∩so␈α⊃restricted␈α∩the␈α⊃function␈α∩has␈α⊃nice
␈↓ ↓H␈↓mathematical properties.

␈↓ ↓H␈↓␈↓¬RID-APPEND: ␈↓  ␈↓↓∀u: u*␈↓¬NIL␈↓↓ = u,     ␈↓
␈↓ ↓H␈↓␈↓¬ASSOC-APPEND: ␈↓  ␈↓↓∀u v w: u*[v*w] = [u*v]*w␈↓.
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *67


␈↓ ↓H␈↓␈↓¬APPEND-NIL␈α␈↓and␈α
␈↓¬RID-APPEND␈α␈↓express␈α
the␈αfact␈αis␈α
that␈α␈↓¬NIL␈↓␈α
is␈αthe␈α"zero"␈α
of␈αthe␈α
append␈αoperation.
␈↓ ↓H␈↓The␈α⊂standard␈α∂mathematical␈α⊂terminology␈α∂is␈α⊂to␈α∂say␈α⊂that␈α⊂␈↓¬NIL␈↓␈α∂is␈α⊂both␈α∂a␈α⊂left␈α∂identity␈α⊂and␈α⊂a␈α∂right
␈↓ ↓H␈↓identity␈αfor␈α␈↓↓append.␈↓␈α ␈↓¬ASSOC-APP␈α␈↓expresses␈αthe␈αfact␈αthat␈α␈↓↓append␈↓␈αis␈αassociative␈α-␈αlike␈αaddition␈αand
␈↓ ↓H␈↓multiplication␈α∀of␈α∀numbers.␈α∪ [Unlike␈α∀addition␈α∀and␈α∀multiplication␈α∪of␈α∀numbers,␈α∀␈↓↓append␈↓␈α∀is␈α∪not
␈↓ ↓H␈↓commutative,␈α
since␈α
␈↓¬(A B)␈↓*␈↓¬(C D)␈↓␈α
=␈α
␈↓¬(A B C D)␈↓␈α
≠␈α
␈↓¬(C D)␈↓*␈↓¬(A B)␈↓].␈α
 Associativity␈α
allows␈α
us␈α
to␈α
omit
␈↓ ↓H␈↓brackets and write ␈↓↓u*v*w,␈↓ because it doesn't matter how the expression is parenthesized.

␈↓ ↓H␈↓        The␈αproofs␈αof␈α␈↓¬APPEND NIL,␈α␈↓and␈α␈↓¬APPEND UU␈α␈↓were␈αuntypically␈αeasy.␈α As␈αwe␈αremarked␈αin␈α␈↓π∞␈↓␈α2
␈↓ ↓H␈↓not␈α∪many␈α∪interesting␈α∪general␈α∪properties␈α∪about␈α∩S-expressions␈α∪or␈α∪lists␈α∪can␈α∪be␈α∪proved␈α∪just␈α∩by
␈↓ ↓H␈↓substituting␈α∃in␈α⊗function␈α∃definitions␈α⊗and␈α∃using␈α⊗known␈α∃properties␈α⊗of␈α∃S-expressions␈α⊗and␈α∃the
␈↓ ↓H␈↓elementary␈αLISP␈α
functions.␈α In␈αfact,␈α
when␈αwe␈αwant␈α
to␈αprove␈αsomething␈α
about␈αa␈α
function␈αdefined
␈↓ ↓H␈↓by␈α
recursion,␈α
we␈α∞almost␈α
always␈α
have␈α∞to␈α
use␈α
some␈α
corresponding␈α∞induction␈α
principle␈α
to␈α∞carry␈α
out
␈↓ ↓H␈↓the␈α⊂proof.␈α⊂ Proving␈α⊂that␈α⊂␈↓¬NIL␈↓␈α⊂is␈α⊃a␈α⊂right␈α⊂identity␈α⊂(␈↓¬RID-APPEND␈↓)␈α⊂is␈α⊂an␈α⊂example.␈α⊃ The␈α⊂induction
␈↓ ↓H␈↓principle␈α∂corresponding␈α∂to␈α∂list␈α∂recursion␈α∂is␈α∞list␈α∂induction.␈α∂ Recall␈α∂that␈α∂list␈α∂recursion␈α∂divides␈α∞the
␈↓ ↓H␈↓computation␈α⊂into␈α∂two␈α⊂cases.␈α⊂ In␈α∂the␈α⊂first␈α⊂case␈α∂the␈α⊂recursion␈α∂argument␈α⊂is␈α⊂␈↓¬NIL␈↓␈α∂and␈α⊂the␈α⊂result␈α∂is
␈↓ ↓H␈↓computed␈α
directly␈α
from␈αthe␈α
parameters.␈α
 In␈αthe␈α
second␈α
case␈α
the␈αrecursion␈α
argument␈α
is␈αa␈α
non-empty
␈↓ ↓H␈↓list␈α∞␈↓↓uu␈↓␈α∞and␈α∞the␈α∂result␈α∞can␈α∞be␈α∞computed␈α∂directly␈α∞once␈α∞it␈α∞is␈α∞known␈α∂for␈α∞␈↓↓␈↓αd|␈↓↓uu␈↓␈α∞Similarly␈α∞to␈α∂prove␈α∞a
␈↓ ↓H␈↓property␈α␈↓↓␈↓πF␈↓↓[u]␈↓␈αusing␈α
list␈αinduction␈αwe␈αconsider␈α
two␈αcases.␈α In␈α
the␈αfirst␈αcase␈α␈↓↓u␈↓␈α
is␈α␈↓¬NIL␈↓␈αand␈α
we␈αmust
␈↓ ↓H␈↓prove␈α
␈↓↓␈↓πF␈↓↓[␈↓¬NIL␈↓↓]␈↓␈α
using␈α
facts␈α
and␈α
definitions␈α
already␈αknown.␈α
 In␈α
the␈α
second␈α
case␈α
␈↓↓u␈↓␈α
an␈α
a␈αnon-empty␈α
list
␈↓ ↓H␈↓␈↓↓uu␈↓␈α
and␈αwe␈α
prove␈α␈↓↓␈↓πF␈↓↓[uu]␈↓␈α
using␈α
in␈αaddition␈α
to␈αknown␈α
facts,␈α
the␈αinduction␈α
hypothesis␈α␈↓↓␈↓πF␈↓↓[␈↓αd|␈↓↓uu]␈↓.␈α
  This
␈↓ ↓H␈↓analysis␈α∞is␈α∂expressed␈α∞in␈α∂the␈α∞schema␈α∂␈↓¬LISTINDUCTION␈α∞␈↓given␈α∂in␈α∞␈↓π∞␈↓␈α∂4.␈α∞  It␈α∂can␈α∞also␈α∂be␈α∞viewed␈α∂as␈α∞a
␈↓ ↓H␈↓proof rule.

␈↓ ↓H␈↓        Let's␈α∀see␈α∪how␈α∀this␈α∪works␈α∀to␈α∪prove␈α∀␈↓↓␈↓πF␈↓↓ u ≡ u*␈↓¬NIL␈↓↓=u␈↓.␈α∪ In␈α∀the␈α∪NIL␈α∀case␈α∪we␈α∀must␈α∪prove
␈↓ ↓H␈↓␈↓↓␈↓¬NIL␈↓↓*␈↓¬NIL␈↓↓=␈↓¬NIL␈↓↓␈↓,␈αwhich␈αis␈αan␈αinstance␈αof␈α␈↓¬APPEND-NIL.␈α␈↓␈αIn␈αthe␈αnon-NIL␈αcase␈αwe␈αhave␈αthe␈αinduction
␈↓ ↓H␈↓hypothesis ␈↓↓␈↓αd|␈↓↓uu*␈↓¬NIL␈↓↓ = ␈↓αd|␈↓↓uu ␈↓, and

␈↓ ↓H␈↓␈↓ αX␈↓↓  uu*␈↓¬NIL␈↓↓ = ␈↓αa|␈↓↓uu . [␈↓αd|␈↓↓uu * ␈↓¬NIL␈↓↓]␈↓␈↓ πX; ␈↓¬APPEND-UU ␈↓
␈↓ ↓H␈↓␈↓ αX␈↓↓          = ␈↓αa|␈↓↓uu . ␈↓αd|␈↓↓uu␈↓
␈↓ ↓H␈↓␈↓ αX␈↓↓          = uu ␈↓␈↓ πX; ␈↓¬CONS ␈↓

␈↓ ↓H␈↓which is the desired proof of ␈↓↓␈↓πF␈↓↓[uu]␈↓ assuming ␈↓↓␈↓πF␈↓↓[␈↓αd|␈↓↓uu]␈↓.

␈↓ ↓H␈↓        A␈α∪similar␈α∪(though␈α∪slightly␈α∪more␈α∪complicated)␈α∪argument␈α∪will␈α∪prove␈α∪the␈α∪associativity␈α∩of
␈↓ ↓H␈↓␈↓↓append␈↓␈α
(␈↓¬ASSOC-APPEND␈↓).␈α
In␈α
particular␈α
we␈α
want␈α
to␈α
prove␈α
␈↓↓␈↓πF␈↓↓ u ≡ u*[v*w]=[u*v]*w␈↓.␈α
 In␈α
the␈αNIL␈α
case
␈↓ ↓H␈↓we␈α
must␈α
prove␈α
␈↓↓␈↓¬NIL␈↓↓*[v*w]=[␈↓¬NIL␈↓↓*v]*w␈↓,␈α
which␈α
follows␈α
easily␈α
using␈α
two␈α
applications␈α
of␈α
␈↓¬APPEND-NIL.
␈↓ ↓H␈↓¬␈↓ In␈α
the␈αnon-NIL␈α
case␈αwe␈α
must␈αprove␈α
␈↓↓uu*[v*w]=[uu*v]*w␈↓.␈α We␈α
assume␈αthe␈α
induction␈αhypothesis:
␈↓ ↓H␈↓␈↓↓␈↓αd|␈↓↓uu*[v*w]=[␈↓αd|␈↓↓uu*v]*w␈↓ We also need the two simple facts

␈↓ ↓H␈↓␈↓¬APPEND-CAR: ␈↓  ␈↓↓␈↓αa|␈↓↓[uu * v] = ␈↓αa|␈↓↓[␈↓αa|␈↓↓uu . [␈↓αd|␈↓↓uu*v]] = ␈↓αa|␈↓↓uu␈↓
␈↓ ↓H␈↓␈↓¬APPEND-CDR: ␈↓  ␈↓↓␈↓αd|␈↓↓uu * v = ␈↓αd|␈↓↓[␈↓αa|␈↓↓uu . [␈↓αd|␈↓↓uu*v]] = ␈↓αd|␈↓↓uu*v␈↓

␈↓ ↓H␈↓which␈α
are␈α
direct␈α
consequences␈α
of␈α
␈↓¬APPEND-UU,␈α∞␈↓domain␈α
facts␈α
and␈α
the␈α
S-expression␈α
facts␈α∞␈↓¬CAR␈α
␈↓and
␈↓ ↓H␈↓␈↓¬CDR. ␈↓ We have

␈↓ ↓H␈↓␈↓ ↓X␈↓↓uu*[v*w]=␈↓αa|␈↓↓uu . ␈↓αd|␈↓↓uu*[v*w]␈↓␈↓ πX; ␈↓¬APPEND-UU ␈↓
␈↓ ↓H␈↓␈↓ ↓X␈↓↓        =␈↓αa|␈↓↓uu . [[␈↓αd|␈↓↓uu*v]*w]␈↓␈↓ πX; induction hypothesis
␈↓ ↓H␈↓␈↓ ↓X␈↓↓        =␈↓αa|␈↓↓[uu*v] . [␈↓αd|␈↓↓[uu*v]*w]␈↓␈↓ πX; ␈↓¬APPEND-CAR,CDR ␈↓
␈↓ ↓H␈↓␈↓ ↓X␈↓↓        =[uu*v]*w]␈↓␈↓ πX; ␈↓¬APPEND-UU ␈↓
␈↓ ↓H␈↓68␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓completing␈αthe␈αproof␈αof␈α␈↓¬ASSOC-APPEND.␈α␈↓␈αThe␈αalgebra␈α
of␈αthis␈αproof␈αis␈αrather␈αtypical.␈αWe␈αused␈α
the
␈↓ ↓H␈↓definition␈αof␈αthe␈α
function␈αinvolved␈αseveral␈α
times,␈αwe␈αused␈αthe␈α
induction␈αhypothesis␈αonce,␈α
and␈αwe
␈↓ ↓H␈↓used␈αthe␈α
elementary␈αalgebraic␈α
facts␈αabout␈α
conditional␈αexpressions␈α
and␈αthe␈α
basic␈αfunctions␈αof␈α
LISP.
␈↓ ↓H␈↓Also␈α∩in␈α∪this␈α∩proof␈α∪it␈α∩does␈α∪not␈α∩matter␈α∩whether␈α∪we␈α∩take␈α∪␈↓↓␈↓πF␈↓↓[u]␈↓␈α∩to␈α∪be␈α∩␈↓↓u*[v*w]=[u*v]*w␈↓␈α∪or␈α∩␈↓↓∀v
␈↓ ↓H␈↓↓w:u*[v*w]=[u*v]*w␈↓.␈α
 The␈α
difference␈α
being␈α
that␈α
in␈α
the␈α
first␈α
case␈α
the␈α
induction␈α
hypothesis␈α
is␈αonly
␈↓ ↓H␈↓refers␈α
to␈αthe␈α
particular␈α␈↓↓v␈↓␈α
and␈α
␈↓↓w␈↓␈αof␈α
the␈αconclusion␈α
while␈α
in␈αthe␈α
second␈αcase␈α
it␈α
applies␈αto␈α
any␈αlists␈α
␈↓↓v,␈↓
␈↓ ↓H␈↓␈↓↓w.␈↓␈α⊂ When␈α⊂proving␈α⊂properties␈α∂of␈α⊂recursively␈α⊂defined␈α⊂functions,␈α∂the␈α⊂unquantified␈α⊂form␈α⊂of␈α⊂␈↓πF␈↓␈α∂is
␈↓ ↓H␈↓generally␈αgood␈α
enough␈αwhen␈α
the␈αfunction␈α
definion␈αdoes␈α
not␈αmodify␈α
the␈αparameters␈α
(non-recursion
␈↓ ↓H␈↓arguments)␈α
in␈αrecursive␈α
calls.␈α If␈α
it␈αpasses␈α
functions␈αof␈α
the␈αparameters␈α
in␈αrecursive␈α
the␈αit␈α
may␈αbe
␈↓ ↓H␈↓necessary to use the quantified version of ␈↓πF␈↓.


␈↓ ↓H␈↓α␈↓ εεExercises.

␈↓ ↓H␈↓Recall that ␈↓↓reverse␈↓ and ␈↓↓reverse1␈↓ are defined by

␈↓ ↓H␈↓        ␈↓↓reverse1 u ← if ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [reverse1 ␈↓αd|␈↓↓u] * <␈↓αa|␈↓↓u>␈↓

␈↓ ↓H␈↓        ␈↓↓reverse u ← rev[u, ␈↓¬NIL␈↓↓]␈↓

␈↓ ↓H␈↓        ␈↓↓rev[u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ rev[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u . v]␈↓

␈↓ ↓H␈↓Show␈α∞that␈α∞the␈α∞programs␈α∞␈↓↓reverse1␈↓␈α∞and␈α∞␈↓↓rev␈↓␈α∞have␈α∞the␈α∞form␈α∞of␈α∞list␈α∞recursion␈α∞and␈α∞give␈α∞the␈α
domain
␈↓ ↓H␈↓mappings and axioms for the corresponding functions.

␈↓ ↓H␈↓Prove the following properties of ␈↓↓reverse␈↓

␈↓ ↓H␈↓1.      ␈↓↓∀u: reverse u = reverse1 u␈↓

␈↓ ↓H␈↓2.      ␈↓↓∀u v: reverse[u * v] = [reverse v] * [reverse u]␈↓

␈↓ ↓H␈↓3.      ␈↓↓∀u: reverse reverse u = u␈↓.

␈↓ ↓H␈↓The first proof requires inventing a suitable sentence on which to do an induction.

␈↓ ↓H␈↓        Now␈α⊂we␈α⊂consider␈α⊂two␈α⊂other␈α⊂classes␈α⊂of␈α⊂programs␈α⊂that␈α⊂terminate␈α⊂on␈α⊂form.␈α⊂ Namely,␈α⊂those
␈↓ ↓H␈↓defined␈α⊃by␈α⊃S-expression␈α⊃rescursion␈α⊃and␈α⊃those␈α⊃defined␈α⊃by␈α⊃numerical␈α⊃recursion.␈α⊃ (The␈α⊃latter␈α⊃is
␈↓ ↓H␈↓generally␈αknown␈αas␈αprimitive␈αrecursion.)  ␈αRecall␈αfrom␈αChapter II␈αthat␈αS-expression␈αrecursion␈αhas
␈↓ ↓H␈↓the form

␈↓ ↓H␈↓5.3)␈↓ α∩␈↓↓   f[x,y] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ g[x,y] ␈↓αelse␈↓↓ h[␈↓αa|␈↓↓x,␈↓αd|␈↓↓x,y,f[␈↓αa|␈↓↓x,j1[␈↓αa|␈↓↓x,␈↓αd|␈↓↓x,y]], f[␈↓αd|␈↓↓x,j2[␈↓αa|␈↓↓x,␈↓αd|␈↓↓x,y]]]␈↓ 

␈↓ ↓H␈↓and numerical recursion has the form

␈↓ ↓H␈↓5.4)␈↓ β+␈↓↓f[n,y] ← ␈↓αif␈↓↓ n ␈↓αequal␈↓↓ ␈↓¬0 ␈↓↓␈↓αthen␈↓↓ g[y] ␈↓αelse␈↓↓ h[n-␈↓¬1␈↓↓,y,f[n-␈↓¬1␈↓↓,j[n-␈↓¬1␈↓↓,y]]]␈↓. 

␈↓ ↓H␈↓As␈α∞for␈α∞list␈α∞recursion,␈α∞the␈α∂symbols␈α∞␈↓↓g,␈↓␈α∞␈↓↓h,␈↓␈α∞␈↓↓j,␈↓␈α∞␈↓↓j1,␈↓␈α∞␈↓↓j2␈↓␈α∂occuring␈α∞in␈α∞these␈α∞definitions␈α∞must␈α∂be␈α∞denote
␈↓ ↓H␈↓explicitly␈α∂definable␈α∂programs␈α∞known␈α∂to␈α∂terminate␈α∂on␈α∞appropriate␈α∂domains␈α∂and␈α∂having␈α∞suitable
␈↓ ↓H␈↓ranges.␈α In␈αparticular␈αfor␈αS-expression␈αrecursion,␈αif␈αthe␈αprograms␈α␈↓↓g,␈↓␈α␈↓↓h,␈↓␈α␈↓↓j1,␈↓␈α␈↓↓j2␈↓␈αare␈αrepresented␈αby
␈↓ ↓H␈↓functions ␈↓↓gg,␈↓ ␈↓↓hh,␈↓ ␈↓↓jj1,␈↓ ␈↓↓jj2␈↓ and we will have the domain mappings:
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *69



␈↓ ↓H␈↓␈↓ βx␈↓↓ gg: SEXP ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β1␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ hh: SEXP ␈↓¬x␈↓↓ SEXP ␈↓¬x␈↓↓ D␈↓β0␈↓↓ ␈↓¬x␈↓↓ D ␈↓¬x␈↓↓ D → D␈↓β2␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓jj1: SEXP ␈↓¬x␈↓↓ SEXP ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β0␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓jj2: SEXP ␈↓¬x␈↓↓ SEXP ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β0␈↓↓␈↓

␈↓ ↓H␈↓and if ␈↓↓ff␈↓ is the function representing ␈↓↓f␈↓ then we add the domain mappings

␈↓ ↓H␈↓␈↓ βx␈↓↓ff: SEXP ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β1␈↓↓ ∪ D␈↓β2␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ff: ATOM ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β1␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ff: PAIR ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β2␈↓↓␈↓

␈↓ ↓H␈↓and the axiom characterizing ␈↓↓ff␈↓ will be:

␈↓ ↓H␈↓␈↓ ↓↑␈↓↓∀x ␈↓πx␈↓↓:ff[x,␈↓πx␈↓↓]=␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ gg[x,␈↓πx␈↓↓] ␈↓αelse␈↓↓  hh[␈↓αa|␈↓↓x,␈↓αd|␈↓↓x,␈↓πx␈↓↓,ff[␈↓αa|␈↓↓x,jj1[␈↓αa|␈↓↓x,␈↓αd|␈↓↓x,␈↓πx␈↓↓]],ff[␈↓αd|␈↓↓x,jj2[␈↓αa|␈↓↓x,␈↓αd|␈↓↓x,␈↓πx␈↓↓]]]␈↓.

␈↓ ↓H␈↓where ␈↓πx␈↓ is of sort ␈↓↓D␈↓β0␈↓↓␈↓.

␈↓ ↓H␈↓and in the case of numeric recursion, we will have the domain mappings:

␈↓ ↓H␈↓␈↓ βx␈↓↓gg: D␈↓β0␈↓↓ → D␈↓β1␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓hh: NATNUM ␈↓¬x␈↓↓ D␈↓β0␈↓↓ ␈↓¬x␈↓↓ D → D␈↓β2␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓jj: NATNUM ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β0␈↓↓␈↓

␈↓ ↓H␈↓and if ␈↓↓ff␈↓ is the function representing ␈↓↓f␈↓ we will add the domain mappings

␈↓ ↓H␈↓␈↓ βx␈↓↓ff: NATNUM ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β1␈↓↓ ∪ D␈↓β2␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ff: ZERO ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β1␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ff: POSP ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β2␈↓↓␈↓

␈↓ ↓H␈↓and the axiom characterizing ␈↓↓ff␈↓ will be:

␈↓ ↓H␈↓␈↓ αz␈↓↓∀n ␈↓πx␈↓↓:ff[n,␈↓πx␈↓↓]=␈↓αif␈↓↓ n ␈↓αequal␈↓↓ ␈↓¬0 ␈↓↓␈↓αthen␈↓↓ gg[␈↓πx␈↓↓] ␈↓αelse␈↓↓ hh[n-␈↓¬1␈↓↓,␈↓πx␈↓↓,ff[n-␈↓¬1␈↓↓,jj[n-␈↓¬1␈↓↓,␈↓πx␈↓↓]]␈↓.

␈↓ ↓H␈↓where ␈↓πx␈↓ is of sort ␈↓↓D␈↓β0␈↓↓␈↓.

␈↓ ↓H␈↓[Remark:␈α As␈αwith␈αlist␈αrecursion,␈αwe␈αhave␈αformulated␈αS-expression␈αand␈αnumeric␈αrecursion␈αwith␈αa
␈↓ ↓H␈↓single parameter.  The extension to several parameters should be clear.]

␈↓ ↓H␈↓α␈↓ εExample

␈↓ ↓H␈↓        To␈α∂illustrate␈α∂the␈α∂use␈α∂of␈α∂the␈α∂S-expression␈α∂recursion␈α∂principle,␈α∂we␈α∂will␈α⊂introduce␈α∂functions
␈↓ ↓H␈↓␈↓↓size,␈↓ ␈↓↓fringe␈↓ and ␈↓↓length␈↓ representing the programs of the same names and prove

␈↓ ↓H␈↓␈↓ ¬~␈↓↓∀x: length fringe x = size x␈↓ 

␈↓ ↓H␈↓(We␈α
assume␈α
at␈α
this␈αpoint␈α
that␈α
"+"␈α
and␈αsimple␈α
properties␈α
of␈α
numbers␈αare␈α
known␈α
and␈α
will␈αmainly
␈↓ ↓H␈↓concentrate on techniques for proving facts about functions on lists and S-expressions.)
␈↓ ↓H␈↓70␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓Recall the programs for ␈↓↓size,␈↓ ␈↓↓fringe␈↓ and ␈↓↓length:␈↓

␈↓ ↓H␈↓␈↓¬SIZE: ␈↓␈↓ αX␈↓↓size x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x␈↓,
␈↓ ↓H␈↓␈↓¬FRINGE: ␈↓␈↓ αX␈↓↓fringe x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <x> ␈↓αelse␈↓↓ fringe ␈↓αa|␈↓↓x * fringe ␈↓αd|␈↓↓x␈↓,
␈↓ ↓H␈↓␈↓¬LENGTH: ␈↓␈↓ αX␈↓↓length u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ ␈↓¬1␈↓↓ + length ␈↓αd|␈↓↓u␈↓

␈↓ ↓H␈↓␈↓↓size␈↓␈αand␈α
␈↓↓fringe␈↓␈αare␈αdefined␈α
by␈αS-expression␈αrecursion␈α
and␈α␈↓↓length␈↓␈αby␈α
list␈αrecursion.␈α
 The␈αdomain
␈↓ ↓H␈↓mappings and axioms for the corresponding functions are:

␈↓ ↓H␈↓␈↓¬SIZE-DEF: ␈↓␈↓ βx␈↓↓∀x: size x  = ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓size: SEXP → POSP␈↓

␈↓ ↓H␈↓␈↓¬FRINGE-DEF: ␈↓␈↓ βx␈↓↓∀x: fringe x = ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ <x> ␈↓αelse␈↓↓ fringe ␈↓αa|␈↓↓x * fringe ␈↓αd|␈↓↓x␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓fringe: SEXP → NELIST␈↓

␈↓ ↓H␈↓␈↓¬LENGTH-DEF: ␈↓␈↓ βx␈↓↓∀u: length u = ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬0 ␈↓↓␈↓αelse␈↓↓ ␈↓¬1␈↓↓+length ␈↓αd|␈↓↓u␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓length: LIST → NATNUM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓length: NULL → ZERO␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓length: NELIST → POSP␈↓

␈↓ ↓H␈↓As immediate corollaries of the definitions we have

␈↓ ↓H␈↓␈↓¬SIZE-ATOM: ␈↓␈↓ βx␈↓↓∀a: size a  = ␈↓¬1 ␈↓↓␈↓
␈↓ ↓H␈↓␈↓¬SIZE-PAIR: ␈↓␈↓ βx␈↓↓∀xx: size xx  = size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x␈↓

␈↓ ↓H␈↓␈↓¬FRINGE-ATOM: ␈↓␈↓ βx␈↓↓∀a: fringe a = <a> ␈↓
␈↓ ↓H␈↓␈↓¬FRINGE-ATOM: ␈↓␈↓ βx␈↓↓∀xx: fringe xx = fringe ␈↓αa|␈↓↓x * fringe ␈↓αd|␈↓↓x␈↓

␈↓ ↓H␈↓␈↓¬LENGTH-NIL: ␈↓␈↓ βx␈↓↓length ␈↓¬NIL␈↓↓ = ␈↓¬0 ␈↓↓␈↓
␈↓ ↓H␈↓␈↓¬LENGTH-UU: ␈↓␈↓ βx␈↓↓∀uu: length uu = ␈↓¬1␈↓↓ + length ␈↓αd|␈↓↓uu␈↓

␈↓ ↓H␈↓Since␈α⊂the␈α∂definitions␈α⊂of␈α∂␈↓↓fringe␈↓␈α⊂and␈α⊂␈↓↓size␈↓␈α∂are␈α⊂S-expression␈α∂recursions,␈α⊂S-expression␈α⊂induction␈α∂is
␈↓ ↓H␈↓appropriate␈αto␈α
carry␈αout␈α
the␈αproof.␈α
 (Recall␈αthe␈α
rule␈αfor␈α
S-expression␈αinduction␈α
given␈αin␈α
␈↓π∞␈↓␈α2.)  ␈α
We
␈↓ ↓H␈↓wish to prove ␈↓↓∀x: ␈↓πF␈↓↓ x␈↓ where:

␈↓ ↓H␈↓␈↓ ∧=␈↓↓␈↓πF␈↓↓[x] ≡ [length fringe x = size x].           ␈↓ 

␈↓ ↓H␈↓In the ATOM case we must prove ␈↓↓∀a: length fringe a = size a␈↓. We have

␈↓ ↓H␈↓␈↓ α8␈↓↓length fringe a = length <a> ␈↓␈↓ ¬xby ␈↓¬FRINGE-ATOM ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓= ␈↓¬1␈↓↓+length ␈↓αd|␈↓↓<a> ␈↓␈↓ ¬xby ␈↓¬LENGTH-UU ␈↓and domain facts
␈↓ ↓H␈↓␈↓ β8␈↓↓= ␈↓¬1␈↓↓+length ␈↓¬NIL␈↓↓ ␈↓␈↓ ¬xby ␈↓¬CDR ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓= ␈↓¬1 ␈↓↓␈↓␈↓ ¬xby ␈↓¬LENGTH-ZERO ␈↓and properties of +.
␈↓ ↓H␈↓␈↓ β8␈↓↓= size a ␈↓␈↓ ¬xby ␈↓¬SIZE-ATOM ␈↓

␈↓ ↓H␈↓which proves the ATOM case.  In the non-ATOM case we assume the induction hypothesis:

␈↓ ↓H␈↓␈↓ β0␈↓↓[length fringe ␈↓αa|␈↓↓xx = size ␈↓αa|␈↓↓xx] ∧ [length fringe ␈↓αd|␈↓↓xx = size ␈↓αd|␈↓↓xx]␈↓ 

␈↓ ↓H␈↓and prove ␈↓↓length fringe xx = size xx␈↓.  We have:
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *71



␈↓ ↓H␈↓␈↓ α8␈↓↓length fringe xx = length[fringe ␈↓αa|␈↓↓xx * fringe ␈↓αd|␈↓↓xx]␈↓␈↓ πXby ␈↓¬FRINGE-PAIR ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ α8␈↓↓size x = size ␈↓αa|␈↓↓x + size ␈↓αd|␈↓↓x ␈↓␈↓ πXby ␈↓¬SIZE-PAIR ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓= [length fringe ␈↓αa|␈↓↓x] + [length fringe ␈↓αd|␈↓↓x]␈↓␈↓ πXby induction hypohesis

␈↓ ↓H␈↓By␈αthe␈αdomain␈αfacts␈α␈↓↓fringe␈α␈↓αa|␈↓↓xx␈↓␈αand␈α␈↓↓fringe␈α␈↓αd|␈↓↓xx␈↓␈α are␈αof␈αsort␈α␈↓↓LIST,␈↓␈αso␈αwe␈αare␈αreduced␈αto␈αproving
␈↓ ↓H␈↓something␈α∂of␈α∂the␈α∂form␈α∂␈↓↓length[u*v]=length u + length v␈↓.␈α∞ This␈α∂suggests␈α∂that␈α∂we␈α∂prove␈α∂a␈α∞general
␈↓ ↓H␈↓lemma to that effect and then the proof will be complete.  We prove

␈↓ ↓H␈↓␈↓ ∧,␈↓↓∀u v:[length[u*v]=length u + length v].      ␈↓ 

␈↓ ↓H␈↓Since␈αthe␈αdefinition␈αof␈α␈↓↓append␈↓␈αis␈αby␈αlist␈αrecursion␈α
we␈αwill␈αuse␈αlist␈αinduction␈αfor␈αthe␈αproof.␈α  In␈α
the
␈↓ ↓H␈↓NIL case we must show

␈↓ ↓H␈↓␈↓ ∧~␈↓↓∀v:[length[␈↓¬NIL␈↓↓*v]=length ␈↓¬NIL␈↓↓ + length v].      ␈↓ 

␈↓ ↓H␈↓We have

␈↓ ↓H␈↓␈↓ α8␈↓↓length[␈↓¬NIL␈↓↓*v] = length v ␈↓␈↓ π8by ␈↓¬APPEND-NIL ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓= ␈↓¬0 ␈↓↓+ length v ␈↓␈↓ π8by properties of ␈↓¬0 ␈↓and "+".
␈↓ ↓H␈↓␈↓ β8␈↓↓= length ␈↓¬NIL␈↓↓ + length v ␈↓␈↓ π8by ␈↓¬LENGTH-NIL. ␈↓

␈↓ ↓H␈↓proving the NIL case.  In the non-NIL case we assume the induction hypothesis

␈↓ ↓H␈↓␈↓ ∧F␈↓↓length [␈↓αd|␈↓↓uu]*v = length ␈↓αd|␈↓↓uu + length v␈↓ 

␈↓ ↓H␈↓and prove ␈↓↓length[uu*v]=length uu+length v␈↓.  Thus

␈↓ ↓H␈↓␈↓ α8␈↓↓length[uu*v] = ␈↓¬1␈↓↓+length qd[uu*v]␈↓␈↓ π8by ␈↓¬LENGTH-UU ␈↓and domain facts
␈↓ ↓H␈↓␈↓ β8␈↓↓= ␈↓¬1␈↓↓ + length [␈↓αd|␈↓↓uu]*v ␈↓␈↓ π8by ␈↓¬APPEND-CDR ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓= ␈↓¬1␈↓↓ + [length ␈↓αd|␈↓↓uu + length v]␈↓␈↓ π8by induction hypothesis.
␈↓ ↓H␈↓␈↓ β8␈↓↓= length uu + length v␈↓␈↓ π8by associativity of + and ␈↓¬LENGTH-UU ␈↓

␈↓ ↓H␈↓this completes the proof of the lemma and thus of the original theorem.

␈↓ ↓H␈↓        As␈αwe␈αmentioned␈αearlier,␈αthere␈αare␈αmany␈αprograms␈αthat␈αterminate␈αfor␈αall␈αallowed␈αinput,␈αbut
␈↓ ↓H␈↓do␈α∞not␈α∂fit␈α∞into␈α∞one␈α∂of␈α∞the␈α∂standard␈α∞forms␈α∞we␈α∂have␈α∞given␈α∂so␈α∞far.␈α∞ We␈α∂could␈α∞give␈α∂more␈α∞general
␈↓ ↓H␈↓forms.␈α
 [A␈α
particularly␈α∞nice␈α
example␈α
can␈α∞be␈α
found␈α
in␈α
Boyer␈α∞and␈α
Moore[1978]].␈α
 Instead␈α∞we␈α
will
␈↓ ↓H␈↓generalize␈α⊂our␈α⊂technique␈α⊂to␈α⊂provide␈α⊂a␈α⊂method␈α⊂of␈α⊂representing␈α⊂programs␈α⊂defined␈α⊂by␈α⊂a␈α∂general
␈↓ ↓H␈↓recursion and show how to prove termination.



␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓1.␈α You␈α
were␈αasked␈α
to␈αwrite␈α
definitions␈αfor␈α
the␈αfunction␈α
␈↓↓mirror␈↓␈αin␈α
the␈αexercises␈α
of␈αChapter␈α
II␈α␈↓π∞␈↓8.
␈↓ ↓H␈↓Use␈α
the␈α
previously␈α
given␈α
definitions␈α
of␈α
␈↓↓fringe␈↓␈α
and␈α
␈↓↓reverse␈↓␈α
(and␈α
any␈α
facts␈α
proved␈α
about␈α∞them␈α
so
␈↓ ↓H␈↓far) to show that your program satisfies

␈↓ ↓H␈↓␈↓ ∧S␈↓↓∀x:[reverse fringe x = fringe mirror x]␈↓. 
␈↓ ↓H␈↓72␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓6.  ␈↓αRepresenting programs not known to terminate.␈↓


␈↓ ↓H␈↓        The␈α
method␈αof␈α
the␈αpreceding␈α
section␈αfor␈α
representing␈αprograms␈α
known␈αto␈α
terminate␈αworks
␈↓ ↓H␈↓well␈αand␈αcould␈αbe␈αextended␈αto␈αcover␈αa␈αlarge␈αclass␈αof␈αprograms.␈α However␈αit␈αis␈αnot␈αpossible␈αto␈αgive
␈↓ ↓H␈↓a␈α∂syntax␈α∞that␈α∂allows␈α∂exactly␈α∞those␈α∂programs␈α∞that␈α∂always␈α∂terminate,␈α∞because␈α∂deciding␈α∂whether␈α∞a
␈↓ ↓H␈↓program␈αterminates␈α
is␈αone␈α
of␈αthe␈αfamous␈α
unsolvable␈αproblems.␈α
 Also,␈αit␈αis␈α
well␈αknown␈α
that␈αLISP
␈↓ ↓H␈↓programs␈α∞do␈α∞not␈α∞always␈α
terminate.␈α∞ (You␈α∞have␈α∞probably␈α
written␈α∞a␈α∞few␈α∞such␈α∞programs␈α
yourself!)
␈↓ ↓H␈↓Many␈α
interesting␈α
programs␈α
terminate␈α∞on␈α
some␈α
but␈α
not␈α
all␈α∞values␈α
of␈α
the␈α
arguments␈α∞(for␈α
example
␈↓ ↓H␈↓␈↓↓eval␈↓)␈α∞and␈α∞we␈α∞would␈α∞like␈α∞to␈α
be␈α∞able␈α∞to␈α∞say␈α∞things␈α∞about␈α
these␈α∞programs.␈α∞ In␈α∞this␈α∞section␈α∞we␈α
will
␈↓ ↓H␈↓show␈α∞how␈α∂LISP␈α∞programs␈α∞defined␈α∂by␈α∞a␈α∞general␈α∂recursion␈α∞schema␈α∞can␈α∂be␈α∞represented␈α∂and␈α∞how
␈↓ ↓H␈↓termination␈α⊗can␈α⊗expressed␈α⊗and␈α⊗proved.␈α∃ The␈α⊗method␈α⊗extends␈α⊗the␈α⊗method␈α⊗of␈α∃representing
␈↓ ↓H␈↓terminating programs.

␈↓ ↓H␈↓        Recall the general form of LISP program given in the previous section

␈↓ ↓H␈↓6.1)␈↓ ¬[␈↓↓f[x]←␈↓πt␈↓↓[f,x]       ␈↓ 

␈↓ ↓H␈↓where␈α∂␈↓πt␈↓␈α∂is␈α∂a␈α∂LISP␈α∂term␈α∞involving␈α∂␈↓↓f␈↓␈α∂as␈α∂a␈α∂"new"␈α∂program␈α∞name.␈α∂ Let␈α∂␈↓↓f␈↓␈α∂also␈α∂denote␈α∂the␈α∞partial
␈↓ ↓H␈↓function␈αcompute␈αby␈α
this␈αprogram.␈α Thus␈α
␈↓↓f x=y␈↓␈αfor␈αthose␈α
␈↓↓x␈↓␈αsuch␈αthat␈α
the␈αprogram␈αterminates␈α
with
␈↓ ↓H␈↓result␈α
␈↓↓y.␈↓␈αAlso,␈α
for␈αsuch␈α
␈↓↓x␈↓␈αthe␈α
equation␈α␈↓↓f x = ␈↓πt␈↓↓[f,x]␈↓␈α
 holds␈αas␈α
an␈αequation␈α
among␈α
program␈αterms
␈↓ ↓H␈↓and␈α
as␈α
an␈α∞equation␈α
in␈α
the␈α∞theory␈α
if␈α
␈↓↓f␈↓␈α
is␈α∞interpreted␈α
as␈α
any␈α∞function␈α
agreeing␈α
with␈α∞the␈α
program
␈↓ ↓H␈↓whenever␈α
it␈αhalts.␈α
 Since␈α
first␈αorder␈α
logic␈α
has␈αthe␈α
built␈α
in␈αassumption␈α
that␈α
a␈αfunction␈α
has␈αa␈α
unique
␈↓ ↓H␈↓value␈αfor␈αeach␈αof␈αvalue␈αof␈αits␈αargument,␈αwe␈αcan't␈αrepresent␈αthe␈αprogram␈αby␈αa␈αpartial␈α
function␈αon
␈↓ ↓H␈↓S-expressions␈αdirectly.␈α
 Suppose␈αwe␈αtry␈α
to␈αchoose␈α
a␈αtotal␈αfunction␈α
on␈αs-expressions␈α
extending␈αthe
␈↓ ↓H␈↓partial␈α∞function␈α∂and␈α∞satisfying␈α∂the␈α∞the␈α∞functional␈α∂equation.␈α∞ The␈α∂following␈α∞example␈α∂shows␈α∞that
␈↓ ↓H␈↓this is not generally possible.


␈↓ ↓H␈↓αOmega example: S-expressions aren't enough.

␈↓ ↓H␈↓        Consider the program ␈↓↓omega␈↓ defined by:

␈↓ ↓H␈↓␈↓¬OMEGA: ␈↓␈↓ ¬#␈↓↓omega x ← omega x . ␈↓¬NIL␈↓↓␈↓. 

␈↓ ↓H␈↓It␈α
is␈α
easy␈α
to␈α
see␈α
that␈α
this␈α
program␈αnever␈α
terminates.␈α
 (In␈α
a␈α
computer␈α
you␈α
would␈α
probably␈α
get␈αan
␈↓ ↓H␈↓overflow␈αof␈α
some␈αkind␈α
as␈αthe␈α
evaluator␈αattempted␈αto␈α
construct␈αan␈α
infinite␈αtree␈α
of␈α␈↓¬NIL␈↓s.␈α)␈α
  Suppose
␈↓ ↓H␈↓we now form the corresponding equation

␈↓ ↓H␈↓6.2)␈↓ ¬$␈↓↓omega x = omega x . ␈↓¬NIL␈↓↓␈↓. 

␈↓ ↓H␈↓and␈αfurther␈αassume␈αthat␈αwe␈αcan␈αassign␈α S-expression␈αvalues␈αto␈α␈↓↓omega[x]␈↓␈αsuch␈αthat␈αthe␈αequation␈αis
␈↓ ↓H␈↓satisfied.␈α⊂ Thus␈α∂by␈α⊂␈↓¬CAR␈α∂␈↓and␈α⊂substitution␈α⊂we␈α∂have␈α⊂␈↓↓␈↓αa|␈↓↓omega x=omega x␈↓.␈α∂ But␈α⊂this␈α⊂contradicts␈α∂the
␈↓ ↓H␈↓theorem ␈↓¬NO-RPLACS ␈↓proved in ␈↓π∞␈↓2.

␈↓ ↓H␈↓        Among␈αother␈α
things␈αthis␈α
tells␈αus␈α
that␈αif␈α
we␈αare␈α
to␈αrepresent␈α
programs␈αby␈αfunctions␈α
satisfying
␈↓ ↓H␈↓the␈αfunctional␈αequation,␈αwe␈αmust␈αhave␈αobjects␈αin␈αthe␈αdomain␈αother␈αthan␈αS-expressions.␈α What␈αwe
␈↓ ↓H␈↓need␈αare␈α
non-S-expression␈αobjects␈α
to␈αassign␈α
as␈αvalues␈αof␈α
the␈αfunction␈α
when␈αthe␈α
program␈αdoesn't
␈↓ ↓H␈↓terminate.␈α∂ It␈α∂turns␈α∞out␈α∂that␈α∂one␈α∞additional␈α∂object␈α∂will␈α∞do.␈α∂ Thus␈α∂we␈α∞extend␈α∂the␈α∂domain␈α∂of␈α∞S-
␈↓ ↓H␈↓expressions␈αby␈αadding␈αthe␈αnon-Sexpression␈αobject␈α␈↓π|␈↓,␈αcalled␈α"bottom".␈α The␈αextended␈αdomain␈αwill
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *73


␈↓ ↓H␈↓be␈α∀denoted␈α∃␈↓↓ESEXP.␈↓␈α∀ The␈α∃value␈α∀␈↓π|␈↓␈α∃will␈α∀be␈α∀assigned␈α∃to␈α∀␈↓↓f x␈↓␈α∃when␈α∀ever␈α∃the␈α∀corresponding
␈↓ ↓H␈↓computation␈αdoes␈αnot␈αterminate.␈α We␈αextend␈αthe␈αbasic␈αLISP␈αfunctions␈α␈↓αa␈↓,␈α␈↓αd␈↓,␈α␈↓αcons␈↓,␈α␈↓αequal␈↓,␈α␈↓αat␈↓,␈α␈↓αn␈↓,␈α␈↓αnot␈↓
␈↓ ↓H␈↓to␈α
␈↓↓ESEXP␈↓␈α
by␈α
requiring␈α
the␈α
value␈α
to␈α
be␈α
␈↓π|␈↓␈α
if␈α
any␈α
of␈α
the␈α
arguments␈α
is␈α
␈↓π|␈↓.␈α
 We␈α
also␈α
require␈α
functions
␈↓ ↓H␈↓representing␈α
programs␈α
to␈α
have␈α
the␈α
value␈α
␈↓π|␈↓␈α
if␈αany␈α
of␈α
the␈α
arguments␈α
is␈α
␈↓π|␈↓.␈α
 This␈α
corresponds␈αto␈α
"call
␈↓ ↓H␈↓by␈αvalue"␈αevaluation␈αwhich␈αis␈αwhat␈αLISP␈αdoes␈αfor␈αprograms␈αdefined␈αin␈αthe␈αmanner␈α
described␈αin
␈↓ ↓H␈↓previous␈αchapters.␈α It␈αis␈αalso␈αknown␈αas␈αthe␈α"strict"␈αextension.␈α Other␈αextensions␈αwill␈αwork,␈α
but␈αwe
␈↓ ↓H␈↓won't␈α
treat␈α
them␈αhere.␈α
  The␈α
only␈α
non-strict␈αfunctions␈α
we␈α
have␈α
are␈α␈↓αif␈↓␈α
␈↓αand␈↓␈α
and␈α
␈↓αor␈↓.␈α ␈↓αif␈↓␈α
is␈α
␈↓π|␈↓␈α
if␈αits
␈↓ ↓H␈↓first␈α∪argument␈α∪is␈α∪␈↓π|␈↓,␈α∪but␈α∪no␈α∪restriction␈α∀is␈α∪made␈α∪on␈α∪the␈α∪second␈α∪and␈α∪third␈α∀arguments.␈α∪ This
␈↓ ↓H␈↓represents␈αthe␈αspecification␈α
that␈α␈↓αif␈↓␈αonly␈αevaluates␈α
one␈αof␈αthe␈α
second␈αor␈αthird␈αarguments␈α
depending
␈↓ ↓H␈↓on␈α
the␈α
outcome␈α
of␈α
the␈α
evaluating␈α
the␈α
first␈α
argument.␈α
 ␈↓αand␈↓,␈α
and␈α
␈↓αor␈↓,␈α
are␈α
extended␈α
by␈α
extending␈α
their
␈↓ ↓H␈↓definitions in terms of ␈↓αif␈↓.

␈↓ ↓H␈↓[Remark:␈α A␈αcomplete␈αdescription␈αof␈αthe␈αtheory␈αof␈αextended␈αS-expressions,␈αand␈αLISP␈αprograms␈αis
␈↓ ↓H␈↓collected in the last section of this chapter.]

␈↓ ↓H␈↓        Given the program (6.1) the representing function ␈↓↓f␈↓ has the following properties:

␈↓ ↓H␈↓(i)  ␈↓↓f X = ␈↓πt␈↓↓[f,X]␈↓ for all arguments.

␈↓ ↓H␈↓(ii)␈α
 ␈↓↓f␈↓␈α
is␈αthe␈α
"least␈α
defined"␈αsuch␈α
function.␈α
 That␈αis,␈α
if␈α
␈↓↓g␈↓␈αis␈α
any␈α
other␈αfunction␈α
satisfying␈α
(i),␈αthen
␈↓ ↓H␈↓whenever ␈↓↓f X␈↓ is defined (eg. not ␈↓π|␈↓) then ␈↓↓g X␈↓ is also defined and ␈↓↓f X = g X␈↓.

␈↓ ↓H␈↓(iii)␈α␈↓↓f x=y␈↓␈αfor␈αsome␈αS-expression␈α␈↓↓y␈↓␈αif␈αand␈αonly␈αif␈αthe␈αprogram␈α␈↓↓f␈↓␈αon␈αinput␈α␈↓↓x␈↓␈αterminates␈αwith␈αresult
␈↓ ↓H␈↓␈↓↓y.␈↓

␈↓ ↓H␈↓If␈α_the␈α↔program␈α_always␈α↔terminates␈α_then␈α↔the␈α_equation␈α↔completely␈α_determines␈α_the␈α↔function.
␈↓ ↓H␈↓Otherwise␈α∩we␈α∩need␈α∩to␈α∩express␈α∩in␈α∩the␈α∩theory␈α∩the␈α∩fact␈α∩expressed␈α∩by␈α∩(ii).␈α∩ This␈α∩is␈α∩done␈α∩by␈α∩a
␈↓ ↓H␈↓␈↓↓minimization␈↓␈α
␈↓↓schema␈↓␈α
which␈α
will␈α
be␈α
described␈α
later.␈α
 If␈α
we␈α
can␈α
prove␈α
that␈α
␈↓↓SEXP f x␈↓␈α
 using␈αonly␈α
the
␈↓ ↓H␈↓functional␈αequation␈αand␈αother␈αpreviously␈αknown␈αfacts,␈αthen␈αwe␈αwill␈αhave␈αshown␈αthat␈αthe␈αprogram
␈↓ ↓H␈↓terminates on input ␈↓↓x.␈↓

␈↓ ↓H␈↓        To␈α
summarize␈αwe␈α
represent␈α
a␈αprogram␈α
defined␈α
by␈αthe␈α
recursion␈α
schema␈α(6.1)␈α
by␈αfunction␈α
on
␈↓ ↓H␈↓the␈α∞domain␈α∞␈↓↓ESEXP␈↓␈α
whose␈α∞value␈α∞is␈α∞that␈α
determined␈α∞by␈α∞the␈α∞program␈α
when␈α∞it␈α∞terminates␈α∞and␈α
␈↓π|␈↓
␈↓ ↓H␈↓otherwise.␈α∪ This␈α∩function␈α∪is␈α∩partially␈α∪characterized␈α∪by␈α∩adding␈α∪the␈α∩axiom␈α∪obtained␈α∪from␈α∩the
␈↓ ↓H␈↓functional␈α⊃equation␈α∩by␈α⊃quantifying␈α∩over␈α⊃the␈α⊃intended␈α∩domains␈α⊃of␈α∩definition␈α⊃and␈α∩adding␈α⊃the
␈↓ ↓H␈↓domain␈α→mappings␈α→requiring␈α→that␈α→the␈α→value␈α→be␈α→␈↓π|␈↓␈α→if␈α→any␈α→of␈α→the␈α→arguments␈α~are.␈α→ The
␈↓ ↓H␈↓characterization is completed by addition of the minimization schema for the recursion equation.

␈↓ ↓H␈↓        We␈α∂can␈α∞further␈α∂generalize␈α∞the␈α∂form␈α∂of␈α∞definition␈α∂by␈α∞allowing␈α∂multiple␈α∂arguments␈α∞and/or
␈↓ ↓H␈↓mutually recursive definitions.  Thus programs

␈↓ ↓H␈↓␈↓ ∧8␈↓↓f␈↓β1␈↓↓[x␈↓β1␈↓↓, ... x␈↓βn␈↓↓] ← ␈↓πt␈↓↓␈↓β1␈↓↓[f␈↓β1␈↓↓, ... ,f␈↓βm␈↓↓,x␈↓β1␈↓↓, ... ,x␈↓βn␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ¬8.
␈↓ ↓H␈↓6.3)␈↓ ¬8.
␈↓ ↓H␈↓␈↓ ¬8.
␈↓ ↓H␈↓␈↓ ∧8␈↓↓f␈↓βm␈↓↓[x␈↓β1␈↓↓, ... x␈↓βn␈↓↓] ← ␈↓πt␈↓↓␈↓βm␈↓↓[f␈↓β1␈↓↓, ... ,f␈↓βm␈↓↓,x␈↓β1␈↓↓, ... ,x␈↓βn␈↓↓]␈↓

␈↓ ↓H␈↓where␈αthe␈α␈↓πt␈↓␈↓βi␈↓␈αare␈α
LISP␈αterms␈αinvolving␈αthe␈α
␈↓↓f␈↓βi␈↓↓␈↓␈αas␈αnew␈αprogram␈α
names␈αare␈αrepresented␈αby␈αadding␈α
to
␈↓ ↓H␈↓74␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓the␈α_theory␈α_new␈α_function␈α_symbols␈α↔nameing␈α_the␈α_programs,␈α_axioms␈α_and␈α_domain␈α↔mappings
␈↓ ↓H␈↓corresponding to the program definitions and an appropriate minimization schema.


␈↓ ↓H␈↓αExample: Termination and correctness of ␈↓↓flatten␈↓α.

␈↓ ↓H␈↓        As␈α
an␈α
example␈α
of␈αthe␈α
method␈α
we␈α
represent␈α
the␈αprogram␈α
␈↓↓flatten,␈↓␈α
which␈α
was␈α
advertised␈αto
␈↓ ↓H␈↓compute␈αthe␈αfringe␈αof␈αan␈αS-expression,␈αprove␈αthat␈αit␈αis␈αterminates␈αfor␈αall␈αS-expressions␈αand␈αshow
␈↓ ↓H␈↓that␈α␈↓↓flatten␈↓␈αdoes␈αindeed␈αcompute␈αthe␈αsame␈αfunction␈αas␈α␈↓↓fringe.␈↓␈α The␈αlatter␈αproof␈αwill␈αshow␈αhow␈αto
␈↓ ↓H␈↓treat␈α↔programs␈α↔defined␈α↔explicitly␈α↔in␈α↔terms␈α↔of␈α↔an␈α↔auxiliary␈α↔program␈α↔that␈α↔uses␈α⊗additional
␈↓ ↓H␈↓parameters.  Recall the definitions

␈↓ ↓H␈↓␈↓¬FLATTEN: ␈↓␈↓ β(␈↓↓flatten[x] ← flat[x, ␈↓¬NIL␈↓↓]              ␈↓
␈↓ ↓H␈↓␈↓¬FLAT: ␈↓␈↓ β(␈↓↓flat[x,u] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x.u ␈↓αelse␈↓↓ flat[␈↓αa|␈↓↓x,flat[␈↓αd|␈↓↓x,u]]]␈↓.

␈↓ ↓H␈↓We add to our theory the axioms

␈↓ ↓H␈↓␈↓¬FLATTEN-DEF: ␈↓␈↓ β(␈↓↓∀x:flatten[x] = flat[x, ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓¬FLAT-DEF: ␈↓␈↓ β(␈↓↓∀x u:flat[x,u] = ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x.u ␈↓αelse␈↓↓ flat[␈↓αa|␈↓↓x,flat[␈↓αd|␈↓↓x,u]]␈↓.
␈↓ ↓H␈↓␈↓ β(␈↓↓flat: BOTTOM ␈↓¬x␈↓↓ ESEXP → BOTTOM␈↓
␈↓ ↓H␈↓␈↓ β(␈↓↓flat: ESEXP ␈↓¬x␈↓↓ BOTTOM → BOTTOM␈↓

␈↓ ↓H␈↓and as immediate corollaries we have

␈↓ ↓H␈↓␈↓¬FLAT-ATOM: ␈↓␈↓ β(␈↓↓∀a u:flat[a,u] = a . u␈↓.
␈↓ ↓H␈↓␈↓¬FLAT-PAIR: ␈↓␈↓ β(␈↓↓∀xx u:flat[xx,u] = flat[␈↓αa|␈↓↓xx,flat[␈↓αd|␈↓↓xx,u]]␈↓

␈↓ ↓H␈↓First we will prove

␈↓ ↓H␈↓␈↓ ∧\␈↓↓∀x u: NELIST flat[x,u].              ␈↓ 

␈↓ ↓H␈↓This␈α∞tells␈α∞us␈α∞that␈α
the␈α∞progam␈α∞for␈α∞␈↓↓flat␈↓␈α
terminates␈α∞for␈α∞all␈α∞input␈α
pairs␈α∞␈↓↓[x,u]␈↓␈α∞and␈α∞further␈α∞that␈α
the
␈↓ ↓H␈↓range␈α∪is␈α∀the␈α∪domain␈α∀␈↓↓NELIST.␈↓␈α∪ Since␈α∀the␈α∪definition␈α∀is␈α∪an␈α∀extended␈α∪form␈α∀of␈α∪S-expression
␈↓ ↓H␈↓recursion we try S-expression induction as a method of proof.

␈↓ ↓H␈↓In␈αthe␈αATOM␈αcase␈αwe␈αhave␈α␈↓↓∀a␈αu:␈αNELIST␈αflat[a,u]␈↓␈α by␈α␈↓¬FLAT-ATOM␈α␈↓and␈αthe␈αdomain␈αmappings
␈↓ ↓H␈↓for ␈↓αcons␈↓.  In the non-ATOM case, we have the induction hypothesis

␈↓ ↓H␈↓␈↓ βz␈↓↓∀u: NELIST flat[␈↓αa|␈↓↓x,u] ∧ ∀u: NELIST flat[␈↓αd|␈↓↓x,u]␈↓. 

␈↓ ↓H␈↓By␈α∞␈↓¬FLAT-PAIR␈α∞␈↓we␈α∞have␈α∞␈↓↓flat[xx,u]␈α∞=␈α∞flat[␈↓αa|␈↓↓xx,flat[␈↓αd|␈↓↓xx,u]]␈↓.␈α∞Using␈α∞the␈α∞d-part␈α∞of␈α∞the␈α∞hypothesis
␈↓ ↓H␈↓we␈α
then␈α
can␈α
apply␈α
the␈αa-part␈α
with␈α
␈↓↓u␈↓␈α
replaced␈α
by␈α␈↓↓flat[␈↓αd|␈↓↓xx,u]␈↓␈α
and␈α
this␈α
completes␈α
the␈α
proof.␈α An
␈↓ ↓H␈↓immediate consequence of this proof and ␈↓¬FLATTEN ␈↓is that ␈↓↓∀x: NELIST flatten x␈↓.

␈↓ ↓H␈↓        Now␈α↔we␈α↔will␈α↔prove␈α↔␈↓↓∀x:␈α↔flatten␈α↔x␈α_=␈α↔fringe␈α↔x␈↓.␈α↔  By␈α↔␈↓¬FLATTEN␈α↔␈↓this␈α↔is␈α↔the␈α_same␈α↔as
␈↓ ↓H␈↓␈↓↓∀x: flat[x,␈↓¬NIL␈↓↓] = fringe x␈↓.␈α∞  The␈α∞simple␈α∞principles␈α∞we␈α∞have␈α∞used␈α∞so␈α∞far␈α∞suggest␈α∞that␈α∞we␈α
proceed
␈↓ ↓H␈↓directly␈αwith␈αa␈αproof␈αby␈αS-expression␈αinduction␈αon␈α␈↓↓x.␈↓␈α This␈αunfortunately␈αarrives␈αat␈αa␈αdead␈αend.
␈↓ ↓H␈↓(If␈αyou␈αhad␈αtrouble␈αprovinge␈α␈↓↓∀u:␈αreverse1␈αu=reverse␈αu␈↓␈αthis␈αmay␈αbe␈αwhy!)␈αThe␈αdifficulty␈αis␈αthat␈αwe
␈↓ ↓H␈↓begin␈αwith␈α
the␈αsecond␈α
argument␈α␈↓¬NIL␈↓,␈α
but␈αafter␈αexpanding␈α
the␈αdefinition␈α
once␈αthis␈α
is␈αno␈αlonger␈α
the
␈↓ ↓H␈↓case.␈α
 Just␈α
as␈α
we␈α
have␈α
to␈αcarry␈α
along␈α
an␈α
extra␈α
variable␈α
in␈αthe␈α
computation␈α
we␈α
also␈α
do␈α
in␈αthe␈α
proof.
␈↓ ↓H␈↓There are several possible routes to take.  We will prove the following more general statement
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *75


␈↓ ↓H␈↓␈↓ ∧F␈↓↓∀x u: flat[x,u] = fringe x * u.            ␈↓ 

␈↓ ↓H␈↓Clearly␈α
the␈α
statement␈α
we␈α
started␈α
to␈α
prove␈α
is␈α∞a␈α
direct␈α
consequence␈α
of␈α
this␈α
one.␈α
 and␈α
now␈α∞we␈α
may
␈↓ ↓H␈↓proceed by a straightforward S-expression induction.   In the ATOM case we have:

␈↓ ↓H␈↓␈↓ ↓x␈↓↓flat[a,u] = a . u␈↓␈↓ ε8by ␈↓¬FLAT-ATOM ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓= <a> * u␈↓␈↓ ε8by ␈↓¬APPEND-NIL,APPEND-UU, ␈↓since ␈↓↓<x> = x . ␈↓¬NIL␈↓↓␈↓.
␈↓ ↓H␈↓␈↓ αx␈↓↓= fringe a * u␈↓␈↓ ε8by ␈↓¬FRINGE-ATOM ␈↓

␈↓ ↓H␈↓In the non-ATOM case we have the induction hypothesis:

␈↓ ↓H␈↓␈↓ βλ␈↓↓∀u: flat[␈↓αa|␈↓↓xx,u] = fringe ␈↓αa|␈↓↓xx * u ∧ ∀u: flat[␈↓αd|␈↓↓xx,u] = fringe ␈↓αd|␈↓↓xx * u␈↓. 

␈↓ ↓H␈↓and we have

␈↓ ↓H␈↓␈↓ ↓x␈↓↓flat[xx,u] = flat[␈↓αa|␈↓↓xx,flat[␈↓αd|␈↓↓xx,u]]␈↓␈↓ ε8; ␈↓¬FLAT-PAIR ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓= flat[␈↓αa|␈↓↓xx,fringe ␈↓αd|␈↓↓xx * u]␈↓␈↓ ε8; induction hypothesis
␈↓ ↓H␈↓␈↓ αx␈↓↓= fringe ␈↓αa|␈↓↓xx * [fringe ␈↓αd|␈↓↓xx * u]␈↓␈↓ ε8; induction hypothesis
␈↓ ↓H␈↓␈↓ αx␈↓↓= fringe xx * u␈↓␈↓ ε8; ␈↓¬ASSOC-APPEND,FRINGE-PAIR ␈↓

␈↓ ↓H␈↓This␈α∪completes␈α∪the␈α∩proof.␈α∪ Notice␈α∪that␈α∪in␈α∩order␈α∪to␈α∪apply␈α∩the␈α∪induction␈α∪hypothesis␈α∪and␈α∩the
␈↓ ↓H␈↓associativity of ␈↓↓append␈↓ it is necessary to check the domain facts for ␈↓↓fringe.␈↓


␈↓ ↓H␈↓αExample: termination of programs defined by list recursion.

␈↓ ↓H␈↓        Suppose we have a program defined by list recursion as follows

␈↓ ↓H␈↓␈↓¬F: ␈↓  ␈↓↓f[u,␈↓πx␈↓↓] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ g[␈↓πx␈↓↓] ␈↓αelse␈↓↓ h[␈↓αa|␈↓↓u,␈↓αd|␈↓↓u,␈↓πx␈↓↓,f[␈↓αd|␈↓↓u,j[␈↓αa|␈↓↓u,␈↓αd|␈↓↓u,␈↓πx␈↓↓]]␈↓.

␈↓ ↓H␈↓and␈α∞that␈α
the␈α∞programs␈α∞␈↓↓g,␈↓␈α
␈↓↓h,␈↓␈α∞and␈α∞␈↓↓j␈↓␈α
 are␈α∞represented␈α
by␈α∞functions␈α∞having␈α
the␈α∞same␈α∞names␈α
such
␈↓ ↓H␈↓that we have the domain mappings:

␈↓ ↓H␈↓␈↓ βx␈↓↓g: D␈↓β0␈↓↓ → D␈↓β1␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓j: SEXP ␈↓¬x␈↓↓ LIST ␈↓¬x␈↓↓ D␈↓β0␈↓↓ → D␈↓β0␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓h: SEXP ␈↓¬x␈↓↓ LIST ␈↓¬x␈↓↓ D␈↓β0␈↓↓ ␈↓¬x␈↓↓ D → D␈↓β2␈↓↓␈↓

␈↓ ↓H␈↓where␈α∂␈↓↓D␈↓β0␈↓↓,D␈↓β1␈↓↓,D␈↓β2␈↓↓␈↓␈α∂are␈α∂domains␈α∂and␈α∂␈↓↓D=D␈↓β1␈↓↓∪D␈↓β2␈↓↓␈↓,␈α∂and␈α∂␈↓πx␈↓␈α∂ranges␈α∂over␈α∂␈↓↓D␈↓β0␈↓↓␈↓.␈α∂ Then␈α∂the␈α∂program␈α∂␈↓↓f␈↓␈α∞is
␈↓ ↓H␈↓represented by a function ␈↓↓f␈↓ satisfying the axiom:

␈↓ ↓H␈↓␈↓¬F-DEF: ␈↓  ␈↓↓∀u ␈↓πx␈↓↓: f[u,␈↓πx␈↓↓]=␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ g[␈↓πx␈↓↓] ␈↓αelse␈↓↓ h[␈↓αa|␈↓↓u,␈↓αd|␈↓↓u,␈↓πx␈↓↓,f[␈↓αd|␈↓↓u,j[␈↓αa|␈↓↓u,␈↓αd|␈↓↓u,␈↓πx␈↓↓]]␈↓.

␈↓ ↓H␈↓which has the following immediated corollaries:

␈↓ ↓H␈↓␈↓¬F-NIL: ␈↓  ␈↓↓∀␈↓πx␈↓↓: f[␈↓¬NIL␈↓↓,␈↓πx␈↓↓]=g[␈↓πx␈↓↓]␈↓.
␈↓ ↓H␈↓␈↓¬F-UU: ␈↓  ␈↓↓∀uu ␈↓πx␈↓↓: f[uu,␈↓πx␈↓↓]=h[␈↓αa|␈↓↓uu,␈↓αd|␈↓↓uu,␈↓πx␈↓↓,f[␈↓αd|␈↓↓uu,j[␈↓αa|␈↓↓uu,␈↓αd|␈↓↓uu,␈↓πx␈↓↓]]␈↓.

␈↓ ↓H␈↓Using␈αlist␈αinduction␈αwe␈αcan␈αprove␈α␈↓↓∀u␈α␈↓πx␈↓↓:D␈αf[u,␈↓πx␈↓↓]␈↓.␈α In␈αthe␈αNIL␈αcase␈αwe␈αhave␈α␈↓↓∀␈↓πx␈↓↓: D f[␈↓¬NIL␈↓↓,␈↓πx␈↓↓]␈↓␈αby␈α␈↓¬F-
␈↓ ↓H␈↓¬NIL␈α␈↓and␈α
the␈αdomain␈αmappings␈α
for␈α␈↓↓g.␈↓␈α In␈α
the␈αnon-NIL␈αcase␈α
we␈αassume␈αthe␈α
induction␈αhypothesis
␈↓ ↓H␈↓76␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓␈↓↓∀␈↓πx␈↓↓: D f[␈↓αd|␈↓↓uu,␈↓πx␈↓↓]␈↓.␈α∪ By␈α∪␈↓¬F-UU␈α∪␈↓we␈α∪have␈α∪␈↓↓f[uu,␈↓πx␈↓↓]=h[␈↓αa|␈↓↓uu,␈↓αd|␈↓↓uu,␈↓πx␈↓↓,f[␈↓αd|␈↓↓uu,j[␈↓αa|␈↓↓uu,␈↓αd|␈↓↓uu,␈↓πx␈↓↓]]]␈↓.␈α∪ Using␈α∩the
␈↓ ↓H␈↓domain␈αmapping␈αfor␈α␈↓↓j␈↓␈αand␈αthe␈αinduction␈αhypothesis␈αwe␈αhave␈α␈↓↓D f[␈↓αd|␈↓↓uu,j[␈↓αa|␈↓↓uu,␈↓αd|␈↓↓uu,␈↓πx␈↓↓]]␈↓␈αand␈αusing
␈↓ ↓H␈↓the␈α∞domain␈α∞mapping␈α∂for␈α∞␈↓↓h␈↓␈α∞we␈α∞have␈α∂␈↓↓D f[uu,␈↓πx␈↓↓]␈↓␈α∞as␈α∞desired.␈α∞  Using␈α∂this␈α∞we␈α∞can␈α∞easily␈α∂show␈α∞the
␈↓ ↓H␈↓further␈α_domain␈α_properties␈α_claimed␈α→for␈α_␈↓↓f,␈↓␈α_␈↓↓∀␈↓πx␈↓↓: D␈↓β1␈↓↓ f[␈↓¬NIL␈↓↓,␈↓πx␈↓↓]␈↓␈α_and␈α→␈↓↓∀uu ␈↓πx␈↓↓: D␈↓β2␈↓↓ f[uu,␈↓πx␈↓↓]␈↓.␈α_  Thus
␈↓ ↓H␈↓representation␈αof␈αprograms␈αdefined␈αby␈αlist␈αrecursion␈αis␈αa␈αspecial␈αcase␈αof␈αthe␈αmore␈αgeneral␈αmethod.
␈↓ ↓H␈↓Showing␈αthat␈αthe␈αS-expression␈αrecursion␈αand␈α
numeric␈αrecursion␈αprinciples␈αare␈αalso␈αspecial␈αcases␈α
is
␈↓ ↓H␈↓similar.   We need only replace list induction by S-expression or numeric induction respectively.



␈↓ ↓H␈↓7.  ␈↓αRecursively Defined Predicates.␈↓


␈↓ ↓H␈↓        In␈α
Chapter␈α
I␈α
we␈α
introduced␈α
propositional␈α∞constructs␈α
␈↓↓and,␈↓␈α
␈↓↓or␈↓␈α
and␈α
␈↓↓not␈↓␈α
into␈α∞the␈α
programing
␈↓ ↓H␈↓language␈αwith␈α
the␈αintent␈α
that␈αthey␈α
should␈αbehave␈α
like␈αthe␈α
usual␈αlogical␈α
operators.␈α We␈αalso␈α
treated
␈↓ ↓H␈↓some␈α∂of␈α∂the␈α∂recursive␈α⊂definitions␈α∂as␈α∂though␈α∂they␈α⊂defined␈α∂predicates␈α∂rather␈α∂than␈α⊂functions␈α∂(for
␈↓ ↓H␈↓example␈α∀␈↓↓member␈↓␈α∀and␈α∀␈↓↓equal).␈↓␈α∀ In␈α∀this␈α∀section␈α∀we␈α∀will␈α∀show␈α∀how␈α∀to␈α∀represent␈α∃programs␈α∀by
␈↓ ↓H␈↓recursively␈α⊂defined␈α⊂predicates.␈α⊂  For␈α⊃many␈α⊂programs␈α⊂written␈α⊂using␈α⊂the␈α⊃propositional␈α⊂constructs
␈↓ ↓H␈↓there␈α∀is␈α∀a␈α∪corresponding␈α∀predicate␈α∀on␈α∀S-expressions␈α∪satisfying␈α∀the␈α∀equivalence␈α∀obtained␈α∪by
␈↓ ↓H␈↓replacing␈α
"←"␈α
by␈α
"≡"␈α
and␈α
the␈α
propositional␈α
programs␈α
by␈α
the␈α
corresponding␈α
logical␈α
connectives␈α
or
␈↓ ↓H␈↓predicates.␈α In␈αgeneral␈αit␈αis␈αnot␈αsafe␈αto␈αmake␈αsuch␈αa␈αtranslation␈αdirectly.␈α To␈αsee␈αwhy␈αconsider␈αthe
␈↓ ↓H␈↓following program:

␈↓ ↓H␈↓␈↓¬LOSE: ␈↓  ␈↓↓lose x ← ␈↓αnot␈↓↓ lose x ␈↓.

␈↓ ↓H␈↓This would translate into

␈↓ ↓H␈↓␈↓¬LOSE-DEF: ␈↓  ␈↓↓∀x:[lose x ≡ ¬lose x] ␈↓.

␈↓ ↓H␈↓which␈α∂clearly␈α∞loses␈α∂by␈α∞leading␈α∂to␈α∂a␈α∞contradiction!␈α∂ We␈α∞avoided␈α∂a␈α∞similiar␈α∂problem␈α∂for␈α∞function
␈↓ ↓H␈↓definitions␈α
by␈α
adding␈α
the␈α
element␈α
␈↓π|␈↓␈α
to␈α∞the␈α
domain␈α
thus␈α
providing␈α
a␈α
non␈α
S-expression␈α∞value␈α
to
␈↓ ↓H␈↓assign␈α⊃to␈α⊃a␈α∩term␈α⊃when␈α⊃the␈α∩computation␈α⊃did␈α⊃not␈α∩produce␈α⊃a␈α⊃value.␈α∩ Our␈α⊃solution␈α⊃is␈α∩here␈α⊃the
␈↓ ↓H␈↓following.␈α∪ We␈α∪continue␈α∪to␈α∪view␈α∪recursive␈α∪definitions␈α∪as␈α∪defining␈α∪functions.␈α∪ Some␈α∪of␈α∪these
␈↓ ↓H␈↓functions␈α
will␈α
be␈α∞intended␈α
to␈α
compute␈α∞predicates␈α
in␈α
the␈α∞sense␈α
that␈α
the␈α∞predicate␈α
is␈α
true␈α∞only␈α
for
␈↓ ↓H␈↓those␈αvalues␈α
of␈αthe␈α
arguments␈αfor␈α
which␈αthe␈αvalue␈α
of␈αthe␈α
function␈αis␈α
an␈αS-expression␈α
other␈αthat
␈↓ ↓H␈↓␈↓¬NIL␈↓.  This is most naturally carried out in terms of a predicate ␈↓↓True␈↓ characterized by the axiom:

␈↓ ↓H␈↓␈↓¬TRUTH: ␈↓  ␈↓↓∀X: [True X ≡ SEXP X ∧ X≠␈↓¬NIL␈↓↓]␈↓

␈↓ ↓H␈↓from which we easily deduce

␈↓ ↓H␈↓␈↓¬TRUTH1: ␈↓  ␈↓↓∀x: [True x ≡ x≠␈↓¬NIL␈↓↓]␈↓

␈↓ ↓H␈↓        For example consider the recursive program

␈↓ ↓H␈↓␈↓¬SUBEXPF: ␈↓  ␈↓↓subexpf[x, y] ← [x ␈↓αequal␈↓↓ y] ␈↓αor␈↓↓ [␈↓αnot␈↓↓ ␈↓αat|␈↓↓y ␈↓αand␈↓↓ [subexpf[x, ␈↓αa|␈↓↓y] ␈↓αor␈↓↓ subexpf[x, ␈↓αd|␈↓↓y]]]␈↓.

␈↓ ↓H␈↓Our intent is to define a predicate ␈↓↓subexp␈↓ satisfying the equivalence
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *77


␈↓ ↓H␈↓␈↓¬SUBEXP: ␈↓  ␈↓↓∀x y: [subexp[x, y] ≡ [x = y] ∨ [¬ATOM y ∧ [subexp[x, ␈↓αa|␈↓↓y] ∨ subexp[x, ␈↓αd|␈↓↓y]]]]␈↓,

␈↓ ↓H␈↓but␈α
we␈α
have␈α
no␈α
rule␈α
that␈α
allows␈α
changing␈α
a␈α
recursive␈α
program␈α
immediately␈α
into␈α
an␈α
equivalence
␈↓ ↓H␈↓relation␈αfor␈αa␈αpredicate.␈α Such␈αa␈αrule␈α
would␈αlose␈αas␈αdescribed␈αabove.␈α Therefore,␈αwe␈αfirst␈α
represent
␈↓ ↓H␈↓the program as a function obtaining the axiom

␈↓ ↓H␈↓␈↓¬SUBEXPF-DEF: ␈↓␈↓↓∀x y:subexpf[x,y]=[x ␈↓αequal␈↓↓ y]␈↓αor␈↓↓[␈↓αnot␈↓↓ ␈↓αat|␈↓↓y ␈↓αand␈↓↓ [subexpf[x,␈↓αa|␈↓↓y] ␈↓αor␈↓↓ subexpf[x,␈↓αd|␈↓↓y]]]␈↓

␈↓ ↓H␈↓and the two immediate corollaries:

␈↓ ↓H␈↓␈↓¬SUBEXPF-ATOM: ␈↓  ␈↓↓∀x a: subexpf[x,a] = x ␈↓αequal␈↓↓ a␈↓
␈↓ ↓H␈↓␈↓¬SUBEXPF-PAIR: ␈↓  ␈↓↓∀x yy: subexpf[x,yy] = x ␈↓αequal␈↓↓ yy ␈↓αor␈↓↓ [subexpf[x,␈↓αa|␈↓↓yy] ␈↓αor␈↓↓ subexpf[x,␈↓αd|␈↓↓yy]].␈↓

␈↓ ↓H␈↓This␈α∞is␈α∞permissible␈α∞without␈α∞first␈α∞proving␈α∂the␈α∞program␈α∞always␈α∞terminates,␈α∞because,␈α∞as␈α∂proved␈α∞in
␈↓ ↓H␈↓(Cartwright␈α∂1976),␈α∂the␈α∂function␈α∂can␈α∂consistently␈α∂be␈α∂regarded␈α∂has␈α∂taking␈α∂the␈α∂value␈α∂␈↓π|␈↓␈α∂when␈α∂the
␈↓ ↓H␈↓program doesn't terminate.

␈↓ ↓H␈↓We prove that ␈↓↓subexpf␈↓ is total

␈↓ ↓H␈↓␈↓¬S-SUBEXP: ␈↓  ␈↓↓∀x y: SEXP subexpf[x, y].       ␈↓

␈↓ ↓H␈↓using S-expression induction on the second argument.  Thus we wish to prove ␈↓↓∀y: ␈↓πF␈↓↓ y␈↓␈↓↓␈↓ where:

␈↓ ↓H␈↓␈↓ ∧9␈↓↓␈↓πF␈↓↓[y] ≡ ∀z: SEXP subexpf[z,y].              ␈↓ 

␈↓ ↓H␈↓This␈αworks␈αbecause␈αrecursive␈αcalls␈αto␈α␈↓↓subexpf␈↓␈αin␈αthe␈αdefinition␈αall␈αinvolve␈α␈↓↓␈↓αa|␈↓↓y␈↓␈αor␈α␈↓↓␈↓αd|␈↓↓y␈↓␈αas␈αthe␈αsecond
␈↓ ↓H␈↓argument.␈α For␈αthe␈αATOM␈αcase␈α ␈↓↓∀z␈αa:␈αSEXP␈αsubexpf[z,a]␈↓␈αfollows␈αfrom␈α␈↓¬SUBEXPF-ATOM␈α␈↓␈αand␈αthe
␈↓ ↓H␈↓domain mapping of ␈↓αequal␈↓.  In the non-ATOM case we have the induction hypothesis

␈↓ ↓H␈↓␈↓ βY␈↓↓∀z: SEXP subexpf[z,␈↓αa|␈↓↓yy] ␈↓αand␈↓↓ ∀z: SEXP subexpf[z,␈↓αd|␈↓↓yy]␈↓ 

␈↓ ↓H␈↓and␈α⊃␈↓↓∀z:␈α⊃SEXP␈α⊃subexpf[z,yy]␈↓␈α⊂follows␈α⊃from␈α⊃this␈α⊃together␈α⊂with␈α⊃␈↓¬SUBEXPF-PAIR␈α⊃␈↓and␈α⊃the␈α⊂domain
␈↓ ↓H␈↓mappings for ␈↓αequal␈↓ and ␈↓αor␈↓.

␈↓ ↓H␈↓If we define the predicate ␈↓↓subexp␈↓ by

␈↓ ↓H␈↓␈↓¬SUBEXP-DEF: ␈↓  ␈↓↓∀X Y: [subexp[X, Y] ≡ True subexpf[X, Y]␈↓.

␈↓ ↓H␈↓we can prove the original sentence proposed for ␈↓↓subexp.␈↓

␈↓ ↓H␈↓        First␈αwe␈αgive␈αcharacterizations␈αof␈αthe␈αpropositional␈αfunctions␈αin␈αterms␈αof␈αthe␈αpredicate␈α␈↓↓True␈↓
␈↓ ↓H␈↓and␈αthe␈αlogical␈αoperations␈αand␈αpredicates␈αthat␈αthe␈αfunctions␈αare␈αindended␈αto␈αimitate.␈α They␈αfollow
␈↓ ↓H␈↓directly from the defining axioms.

␈↓ ↓H␈↓␈↓¬EQNOT: ␈↓␈↓ βx␈↓↓∀x: [True ␈↓αnot␈↓↓ x ≡ ¬True x]␈↓
␈↓ ↓H␈↓␈↓¬EQAND: ␈↓␈↓ βx␈↓↓∀x y: [True[x ␈↓αand␈↓↓ y]≡ [True x ∧ True y]]␈↓
␈↓ ↓H␈↓␈↓¬EQOR: ␈↓␈↓ βx␈↓↓∀x y: [True[x ␈↓αor␈↓↓ y] ≡ [True x ∨ True y]]␈↓
␈↓ ↓H␈↓␈↓¬EQEQUAL: ␈↓␈↓ βx␈↓↓∀x y: [True[x ␈↓αequal␈↓↓ y] ≡ x = y]␈↓
␈↓ ↓H␈↓␈↓¬EQATOM: ␈↓␈↓ βx␈↓↓∀x: [True[␈↓αat|␈↓↓x] ≡ ATOM x]␈↓
␈↓ ↓H␈↓␈↓¬EQNULL: ␈↓␈↓ βx␈↓↓∀x: [True[␈↓αn|␈↓↓x] ≡ NULL x]␈↓
␈↓ ↓H␈↓78␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓The␈αproof␈α
of␈α␈↓¬SUBEXP␈α
␈↓is␈αjust␈αa␈α
sequence␈αof␈α
simplifications␈αbased␈αon␈α
the␈αuse␈α
the␈α␈↓¬EQ-␈α
␈↓lemmas,␈αthe
␈↓ ↓H␈↓definitions and the totality of ␈↓↓subexpf.␈↓  In particular, by ␈↓¬SUBEXP-DEF ␈↓we need only show

␈↓ ↓H␈↓␈↓ ↓r␈↓↓∀x y:[True subexpf[x,y] ≡ [x=y] ∨ [¬ATOM y ∧ [True subexpf[x,␈↓αa|␈↓↓y] ∨ True subexp[x,␈↓αd|␈↓↓y]]]]␈↓ 

␈↓ ↓H␈↓In the ATOM case by ␈↓¬SUBEXPF-ATOM ␈↓

␈↓ ↓H␈↓␈↓ ∧⎇␈↓↓ ∀x a: [True[x ␈↓αequal␈↓↓ a] ≡ x = a]␈↓ 

␈↓ ↓H␈↓which␈α⊂is␈α∂true␈α⊂by␈α∂␈↓¬EQEQUAL.␈α⊂␈↓␈α∂In␈α⊂the␈α⊂non-ATOM␈α∂case␈α⊂we␈α∂let␈α⊂␈↓↓y=yy␈↓␈α∂and␈α⊂by␈α⊂␈↓¬SUBEXPF-PAIR,␈α∂␈↓␈↓¬S-
␈↓ ↓H␈↓¬SUBEXPF ␈↓we need only show

␈↓ ↓H␈↓␈↓ β→␈↓↓∀x yy: [True [ [x ␈↓αequal␈↓↓ yy] ␈↓αor␈↓↓ [subexpf[x,␈↓αa|␈↓↓yy] ␈↓αor␈↓↓ subexpf[x,␈↓αd|␈↓↓yy]] ]␈↓
␈↓ ↓H␈↓␈↓ β~␈↓↓      ≡ [x = yy] ∨ True subexpf[x,␈↓αa|␈↓↓yy] ∨ True subexpf[x,␈↓αd|␈↓↓yy] ]     ␈↓

␈↓ ↓H␈↓But this follows from ␈↓¬S-SSUBEXPF, ␈↓␈↓¬EQOR, ␈↓and ␈↓¬EQEQUAL. ␈↓

␈↓ ↓H␈↓[Remark:␈α
Our␈α
method␈α
of␈αinterpreting␈α
functions␈α
as␈α
predicates␈αwas␈α
chosen␈α
to␈α
correspond␈α
with␈αthe
␈↓ ↓H␈↓usual␈α∩LISP␈α∩convention.␈α∩ Other␈α∩conventions␈α∪are␈α∩possible.␈α∩ For␈α∩example␈α∩we␈α∩could␈α∪restrict␈α∩the
␈↓ ↓H␈↓functions␈αthat␈αare␈αto␈αbe␈αinterpreted␈αas␈αpredicates␈αto␈αhave␈αvalues␈αin␈αthe␈αdomain␈α{␈↓¬T␈↓,␈α␈↓¬NIL␈↓,␈α␈↓π|␈↓}.␈α The
␈↓ ↓H␈↓axioms for ␈↓αand␈↓ etc. would be slightly different, but not much else changes.  ]

␈↓ ↓H␈↓α␈↓ ε⊂Exercise

␈↓ ↓H␈↓        You␈αwere␈αasked␈α
to␈αwrite␈αthe␈αdefinition␈α
for␈αthe␈αpredicate␈α␈↓↓istail␈↓␈α
in␈αthe␈αexercises␈αof␈α
Chapter II
␈↓ ↓H␈↓␈↓π∞␈↓8.  Prove the following facts about ␈↓↓istail.␈↓

␈↓ ↓H␈↓␈↓ βλ␈↓↓∀v: istail[␈↓¬NIL␈↓↓,v]␈↓
␈↓ ↓H␈↓␈↓ βλ␈↓↓∀u v: [¬NULL u ∧ istail[u,v] ⊃ istail[␈↓αd|␈↓↓u,v]]␈↓
␈↓ ↓H␈↓␈↓ βλ␈↓↓∀u v w: [istail[u,v] ∧ istail[v,w] ⊃ istail[u,w]]␈↓

␈↓ ↓H␈↓First␈α
translate␈αyour␈α
definition␈αof␈α
␈↓↓istail␈↓␈α
into␈αa␈α
functional␈αequation␈α
for␈αthe␈α
function␈α
␈↓↓istailf.␈↓␈α Show
␈↓ ↓H␈↓␈↓↓istailf␈↓␈α⊂is␈α⊂total␈α⊂and␈α⊂that␈α⊂␈↓↓istail␈↓␈α⊂satisfies␈α⊂the␈α⊂equivalence␈α⊂you␈α⊂intended.␈α⊂ All␈α⊂of␈α⊂this␈α⊂parallels␈α⊂the
␈↓ ↓H␈↓␈↓↓subexp␈↓ example above.  For the rest you are on your own.



␈↓ ↓H␈↓8.  ␈↓αAdditional induction principles for proving.␈↓


␈↓ ↓H␈↓        When␈α∪we␈α∪presented␈α∪the␈α∪theory␈α∪of␈α∩LISP␈α∪we␈α∪introduced␈α∪princples␈α∪of␈α∪induction␈α∪for␈α∩S-
␈↓ ↓H␈↓expressions,␈αlists,␈α
and␈αnumbers␈αbased␈α
on␈αthe␈α
structure␈αof␈αthe␈α
respective␈αdomains.␈α These␈α
principles
␈↓ ↓H␈↓work␈α
well␈α∞for␈α
proving␈α
facts␈α∞about␈α
functions␈α∞defined␈α
by␈α
the␈α∞corresponding␈α
recursions.␈α∞ They␈α
are
␈↓ ↓H␈↓not␈α∂generally␈α∞easy␈α∂to␈α∂use␈α∞other␈α∂forms␈α∂of␈α∞recursion␈α∂are␈α∞used␈α∂in␈α∂the␈α∞function␈α∂definition.␈α∂ In␈α∞this
␈↓ ↓H␈↓section we will present some additional principles of induction.
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *79


␈↓ ↓H␈↓αInduction on well-founded orderings

␈↓ ↓H␈↓        The␈α⊃general␈α⊂idea␈α⊃of␈α⊃induction␈α⊂on␈α⊃some␈α⊃domain␈α⊂involves␈α⊃a␈α⊃notion␈α⊂of␈α⊃"smaller"␈α⊃on␈α⊂that
␈↓ ↓H␈↓domain␈α∂which␈α∞is␈α∂␈↓↓well-founded.␈↓␈α∞ By␈α∂well-founded␈α∞we␈α∂mean␈α∞that␈α∂every␈α∞sequence␈α∂of␈α∂elements␈α∞in
␈↓ ↓H␈↓which␈α
each␈α
element␈α
is␈α
smaller␈αthan␈α
the␈α
previous␈α
one␈α
must␈αterminate␈α
after␈α
a␈α
finite␈α
number␈αof␈α
steps.
␈↓ ↓H␈↓Given␈αa␈αwell-founded␈αnotion␈αof␈αsmaller␈αand␈αa␈αproperty␈α␈↓πF␈↓,␈αif␈αit␈αis␈αthe␈αcase␈αthat␈αwhenever␈α␈↓πF␈↓␈αholds
␈↓ ↓H␈↓for␈αall␈αelements␈αsmaller␈αthan␈αa␈αgiven␈αelement␈αthen␈αit␈αalso␈αholds␈αfor␈αthat␈αelement,␈αthen␈αwe␈αhave␈α␈↓πF␈↓
␈↓ ↓H␈↓holds for all elements of the domain.  This can be formally expressed by the schema

␈↓ ↓H␈↓␈↓ ∧3␈↓↓∀␈↓πx␈↓↓: [∀␈↓πx␈↓↓␈↓β1␈↓↓: [R[␈↓πx␈↓↓␈↓β1␈↓↓,␈↓πx␈↓↓] ⊃ ␈↓πF␈↓↓ ␈↓πx␈↓↓␈↓β1␈↓↓] ⊃ ␈↓πF␈↓↓ ␈↓πx␈↓↓] ⊃ ∀␈↓πx␈↓↓: ␈↓πF␈↓↓ ␈↓πx␈↓↓␈↓ 

␈↓ ↓H␈↓where␈α⊃␈↓↓R␈↓␈α⊃is␈α⊃a␈α⊃well-founded␈α⊃relation␈α⊃on␈α∩the␈α⊃domain␈α⊃and␈α⊃␈↓πx␈↓,␈↓πx␈↓␈↓β1␈↓␈α⊃are␈α⊃variables␈α⊃ranging␈α∩over␈α⊃the
␈↓ ↓H␈↓domain.

␈↓ ↓H␈↓        To␈α
see␈α
that␈α
this␈α
induction␈α
principle␈α
is␈α
"correct"␈α
suppose␈α
the␈α
hypothesis␈α
holds␈α
and␈α
suppose
␈↓ ↓H␈↓there␈α∞is␈α∞some␈α∞␈↓πx␈↓␈α∞such␈α∞that␈α∞␈↓↓¬␈↓πF␈↓↓ ␈↓πx␈↓↓␈↓.␈α∞ Then␈α∞since␈α∞␈↓↓R␈↓␈α∞is␈α∞well-founded,␈α∞there␈α∞is␈α∞a␈α∞smallest␈α∞␈↓πx␈↓␈α∞such␈α∞that
␈↓ ↓H␈↓␈↓↓¬␈↓πF␈↓↓ ␈↓πx␈↓↓␈↓.␈α
 Otherwise␈αwe␈α
could␈αfind␈α
smaller␈αcounter␈α
examples␈αad-infinitum,␈α
forming␈αa␈α
nonterminating
␈↓ ↓H␈↓sequence␈α∂of␈α∂elements␈α⊂each␈α∂of␈α∂which␈α∂is␈α⊂smaller␈α∂(as␈α∂measured␈α∂by␈α⊂␈↓↓R␈↓)␈α∂than␈α∂its␈α⊂predecessor.␈α∂ This
␈↓ ↓H␈↓contradicts␈α
the␈α
well-foundedness␈αof␈α
␈↓↓R.␈↓␈α
 If␈α
␈↓πx␈↓␈αis␈α
the␈α
smallest␈α
counter␈αexample␈α
then␈α
␈↓↓∀␈↓πx␈↓↓␈↓β1␈↓↓:␈α
R[␈↓πx␈↓↓␈↓β1␈↓↓,␈↓πx␈↓↓]␈α⊃␈α
␈↓πF␈↓↓
␈↓ ↓H␈↓↓␈↓πx␈↓↓␈↓β1␈↓↓␈↓  therefore ␈↓↓␈↓πF␈↓↓ ␈↓πx␈↓↓␈↓ must hold after all.  Hence there is no such counter example.

␈↓ ↓H␈↓        As examples of well-founded relations on domains of immediate interest we have

␈↓ ↓H␈↓␈↓ αh<  the usual order relation on numbers
␈↓ ↓H␈↓␈↓ αh<␈↓βl␈↓ on lists where ␈↓↓u <␈↓βl␈↓↓ v ≡ ∃ww: v=ww*u␈↓, e.g. ␈↓↓u␈↓ is a proper tail of ␈↓↓v.␈↓
␈↓ ↓H␈↓␈↓ αh<␈↓βS␈↓ on S-expressions where ␈↓↓x <␈↓βS␈↓↓ y ≡ x≠y ∧ subexp[x,y]␈↓,
␈↓ ↓H␈↓␈↓ αh         e.g. ␈↓↓x␈↓ is a proper subexpression of ␈↓↓y.␈↓

␈↓ ↓H␈↓Corresponding to these relations we have the following induction schemata:

␈↓ ↓H␈↓␈↓¬NUMBINDUCTION-CVI: ␈↓␈↓ ∧x␈↓↓∀n: [∀m: [m < n ⊃ ␈↓πF␈↓↓ m] ⊃ ␈↓πF␈↓↓ n] ⊃ ∀n: ␈↓πF␈↓↓ n␈↓
␈↓ ↓H␈↓␈↓¬LISTINDUCTION-CVI: ␈↓␈↓ ∧8␈↓↓∀u: [∀v: [v <␈↓βl␈↓↓ u ⊃ ␈↓πF␈↓↓ v] ⊃ ␈↓πF␈↓↓ u] ⊃ ∀u: ␈↓πF␈↓↓ u␈↓
␈↓ ↓H␈↓␈↓¬SEXPINDUCTION-CVI: ␈↓␈↓ ∧x␈↓↓∀x: [∀y: [y <␈↓βS␈↓↓ x ⊃ ␈↓πF␈↓↓ y] ⊃ ␈↓πF␈↓↓ x] ⊃ ∀x: ␈↓πF␈↓↓ x␈↓

␈↓ ↓H␈↓        Given␈α⊂domains␈α⊂␈↓↓D␈↓β1␈↓↓␈↓␈α⊂and␈α⊂␈↓↓D␈↓β2␈↓↓␈↓␈α⊂with␈α⊃well-founded␈α⊂orderings␈α⊂<␈↓β1␈↓,␈α⊂<␈↓β2␈↓␈α⊂respectively␈α⊂there␈α⊃are␈α⊂a
␈↓ ↓H␈↓number␈α
of␈αways␈α
to␈αconstruct␈α
new␈αorderings␈α
on␈αcombinations␈α
of␈αthese␈α
domains.␈α For␈α
example␈αthe
␈↓ ↓H␈↓lexicographic ordering <␈↓β1␈↓␈↓β2␈↓ on ␈↓↓D␈↓β1␈↓↓ ␈↓¬x␈↓↓ D␈↓β2␈↓↓␈↓ is defined by

␈↓ ↓H␈↓␈↓ β[␈↓↓(␈↓πx␈↓↓␈↓β1␈↓↓,␈↓πx␈↓↓␈↓β2␈↓↓) <␈↓β1␈↓↓␈↓β2␈↓↓ (␈↓πx␈↓↓␈↓β1␈↓↓',␈↓πx␈↓↓␈↓β2␈↓↓') ≡ [␈↓πx␈↓↓␈↓β1␈↓↓ <␈↓β1␈↓↓ ␈↓πx␈↓↓␈↓β1␈↓↓' ∨ [␈↓πx␈↓↓␈↓β1␈↓↓=␈↓πx␈↓↓␈↓β1␈↓↓' ∧ ␈↓πx␈↓↓␈↓β2␈↓↓ <␈↓β2␈↓↓ ␈↓πx␈↓↓␈↓β2␈↓↓']]␈↓ 

␈↓ ↓H␈↓Another␈α
means␈αof␈α
defining␈α
a␈αwell-founded␈α
ordering␈αis␈α
to␈α
induce␈αone␈α
by␈α
a␈αmapping␈α
into␈αa␈α
domain
␈↓ ↓H␈↓that␈αhas␈αa␈αwell-founded␈αordering.␈α Thus␈αif␈α␈↓↓␈↓πr␈↓↓:␈αD␈↓β1␈↓↓␈α→␈αD␈↓β2␈↓↓␈↓␈αand␈α <␈↓β2␈↓␈αis␈αa␈αwell-founded␈αordering␈αon␈α␈↓↓D␈↓β2␈↓↓␈↓
␈↓ ↓H␈↓then the ordering <␈↓βr␈↓ on ␈↓↓D␈↓β1␈↓↓␈↓ defined by ␈↓↓␈↓πx␈↓↓ <␈↓βr␈↓↓ ␈↓πx␈↓↓' ≡ ␈↓πr␈↓↓ ␈↓πx␈↓↓ <␈↓β2␈↓↓ ␈↓πr␈↓↓ ␈↓πx␈↓↓'␈↓ is well-founded.
␈↓ ↓H␈↓80␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓αRank functions and Induction on rank.

␈↓ ↓H␈↓        Suppose␈α␈↓↓f␈↓␈αis␈αa␈αrecursively␈αdefined␈αprogram␈αon␈α
some␈αdomain␈α␈↓↓D␈↓␈α and␈αlet␈α␈↓πx␈↓␈αbe␈αa␈α
variable␈αof
␈↓ ↓H␈↓sort␈α
␈↓↓D.␈↓␈α
 Suppose␈αfurther␈α
that␈α
the␈αrecursive␈α
calls␈α
to␈α
␈↓↓f␈↓␈αin␈α
the␈α
definition␈αhave␈α
arguments␈α
of␈αthe␈α
form
␈↓ ↓H␈↓␈↓↓g␈↓β1␈↓↓ ␈↓πx␈↓↓, g␈↓β2␈↓↓ ␈↓πx␈↓↓, ... g␈↓βn␈↓↓ ␈↓πx␈↓↓␈↓.␈α∂ If␈α∂a␈α∂recursion␈α∂is␈α∂to␈α∂succeed␈α∂the␈α∂recursive␈α∂calls␈α∂should␈α∂be␈α∂with␈α∞arguments
␈↓ ↓H␈↓which␈α
are␈α
smaller␈α
in␈α
some␈α
sense.␈α One␈α
way␈α
to␈α
make␈α
the␈α
notion␈αof␈α
smaller␈α
precise␈α
is␈α
to␈α
find␈αa␈α
"rank
␈↓ ↓H␈↓function␈α"␈α
␈↓↓␈↓πr␈↓↓: D → NATNUM␈↓␈αsuch␈αthat␈α
␈↓↓␈↓πr␈↓↓ g␈↓βi␈↓↓ ␈↓πx␈↓↓ < ␈↓πr␈↓↓ ␈↓πx␈↓↓␈↓␈αfor␈α␈↓↓1≤i≤n␈↓.␈α
 This␈αrank␈αfunction␈α
can␈αbe␈αused␈α
to
␈↓ ↓H␈↓prove␈α⊗properties␈α⊗of␈α⊗the␈α⊗function␈α⊗compute␈α⊗by␈α∃␈↓↓f␈↓␈α⊗using␈α⊗the␈α⊗principle␈α⊗of␈α⊗"induction␈α⊗on␈α∃␈↓πr␈↓".
␈↓ ↓H␈↓Informally,␈αthis␈αprinciple␈αsays␈αthat␈αif␈αwhenever␈α␈↓↓␈↓πF␈↓↓␈α␈↓πx␈↓↓␈↓β1␈↓↓␈↓␈αholds␈αfor␈αall␈α␈↓πx␈↓␈↓β1␈↓␈αof␈αlesser␈αrank␈αthan␈α␈↓πx␈↓␈αthen␈α␈↓πx␈↓
␈↓ ↓H␈↓satisfies ␈↓πF␈↓, then all ␈↓πx␈↓ satisfy ␈↓πF␈↓.  Formally we have the schema:

␈↓ ↓H␈↓␈↓¬RANKIND: ␈↓␈↓ ∧(␈↓↓∀␈↓πx␈↓↓: [∀␈↓πx␈↓↓␈↓β1␈↓↓:[␈↓πr␈↓↓ ␈↓πx␈↓↓␈↓β1␈↓↓ < ␈↓πr␈↓↓ ␈↓πx␈↓↓ ⊃ ␈↓πF␈↓↓ ␈↓πx␈↓↓␈↓β1␈↓↓] ⊃ ␈↓πF␈↓↓ ␈↓πx␈↓↓] ⊃ ∀␈↓πx␈↓↓: ␈↓πF␈↓↓ ␈↓πx␈↓↓␈↓ 

␈↓ ↓H␈↓One␈α
way␈α
to␈αjustify␈α
the␈α
schema␈α
is␈αas␈α
an␈α
instance␈α
of␈αinduction␈α
on␈α
a␈α
well-founded␈αordering.␈α
 Namely
␈↓ ↓H␈↓the ordering <␈↓βr␈↓ defined by

␈↓ ↓H␈↓␈↓ ¬I␈↓↓␈↓πx␈↓↓␈↓β1␈↓↓ <␈↓βr␈↓↓ ␈↓πx␈↓↓ ≡ ␈↓πr␈↓↓ ␈↓πx␈↓↓␈↓β1␈↓↓ < ␈↓πr␈↓↓ ␈↓πx␈↓↓␈↓ 

␈↓ ↓H␈↓is␈α
well-founded.␈α
 (Any␈α
strictly␈α∞decreasing␈α
<␈↓βr␈↓ sequence␈α
induces␈α
a␈α
corresponding␈α∞strictly␈α
decreasing
␈↓ ↓H␈↓< sequence.)␈α≠It␈α≠can␈α≠also␈α≠be␈α≠deduced␈α≠in␈α≠our␈α≠theory␈α≠from␈α≠an␈α≠appropriate␈α≤instance␈α≠of
␈↓ ↓H␈↓␈↓¬NATNUMINDUCTION-CVI. ␈↓ Before we prove that, we show how "rank induction" can be applied.

␈↓ ↓H␈↓        Consider the program ␈↓↓gopher␈↓ defined by

␈↓ ↓H␈↓␈↓¬GOPHER: ␈↓    ␈↓↓gopher x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ gopher [␈↓αaa|␈↓↓x . [␈↓αda|␈↓↓x . ␈↓αd|␈↓↓x]]␈↓.

␈↓ ↓H␈↓We represent this program by the function ␈↓↓gopher␈↓ satisfying the axiom:

␈↓ ↓H␈↓␈↓¬GOPHER-DEF: ␈↓    ␈↓↓∀x: gopher x = ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ gopher [␈↓αaa|␈↓↓x . [␈↓αda|␈↓↓x . ␈↓αd|␈↓↓x]]␈↓.

␈↓ ↓H␈↓which has the immediate consequences:

␈↓ ↓H␈↓␈↓¬GOPHER-ATOM: ␈↓␈↓ β(␈↓↓∀xx: [ATOM ␈↓αa|␈↓↓xx ⊃ gopher xx=xx]␈↓
␈↓ ↓H␈↓␈↓¬GOPHER-PAIR: ␈↓␈↓ β(␈↓↓∀xx: [PAIR ␈↓αa|␈↓↓xx ⊃ gopher xx=gopher[␈↓αaa|␈↓↓xx . [␈↓αda|␈↓↓xx . ␈↓αd|␈↓↓xx]]]␈↓

␈↓ ↓H␈↓␈↓↓gopher␈↓␈α∞digs␈α∞up␈α∞the␈α∞left␈α∞most␈α∞atom␈α∞of␈α∞its␈α∞argument,␈α∞stacking␈α∞the␈α∞d-parts␈α∞of␈α∞successive␈α∞␈↓↓car␈↓s␈α∞as␈α∞it
␈↓ ↓H␈↓goes.␈α∞ If␈α∞you␈α∞are␈α∞familiar␈α∞with␈α∞operations␈α
on␈α∞binary␈α∞trees,␈α∞␈↓↓gopher␈↓␈α∞can␈α∞be␈α∞viewed␈α∞as␈α
performing
␈↓ ↓H␈↓successive␈α∞clock-wise␈α∞rotations.␈α
 These␈α∞operations␈α∞preserve␈α
the␈α∞symmetric␈α∞order␈α
on␈α∞leaves␈α∞of␈α
the
␈↓ ↓H␈↓tree.  This corresponds to preserving the fringe of an S-expression.

␈↓ ↓H␈↓        From␈αthe␈αdefinition␈αwe␈α
see␈αthat␈α␈↓↓gopher␈↓␈αmakes␈α
sense␈αonly␈αon␈αnon-atomic␈α
S-expressions.␈α In
␈↓ ↓H␈↓the␈α∀recursive␈α∀call␈α∀␈↓↓gopher[␈↓αaa|␈↓↓x . [␈↓αda|␈↓↓x . ␈↓αd|␈↓↓x]]␈↓␈α∀we␈α∀see␈α∃that␈α∀what␈α∀is␈α∀getting␈α∀"smaller"␈α∀is␈α∃␈↓↓␈↓αa|␈↓↓x␈↓.␈α∀In
␈↓ ↓H␈↓particular

␈↓ ↓H␈↓␈↓¬GOPHER-RANK: ␈↓    ␈↓↓∀xx: [PAIR ␈↓αa|␈↓↓xx ⊃ size ␈↓αa|␈↓↓[␈↓αaa|␈↓↓xx . [␈↓αda|␈↓↓xx . ␈↓αd|␈↓↓xx]] < size ␈↓αa|␈↓↓xx.]␈↓

␈↓ ↓H␈↓So␈α␈↓↓␈↓πr␈↓↓ xx=size ␈↓αa|␈↓↓xx␈↓␈αis␈αan␈αappropriate␈αrank␈αfunction␈αfor␈αproving␈αfacts␈αabout␈αgopher.␈α We␈αwill␈αprove
␈↓ ↓H␈↓the following simple facts:
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *81



␈↓ ↓H␈↓␈↓¬PAIR-GOPHER: ␈↓␈↓ β(␈↓↓∀xx: PAIR gopher xx␈↓
␈↓ ↓H␈↓␈↓¬SIZE-GOHPER: ␈↓␈↓ β(␈↓↓∀xx: size gopher xx = size xx␈↓

␈↓ ↓H␈↓To prove ␈↓↓∀xx: PAIR gopher xx␈↓ we assume the rank induction hypothesis

␈↓ ↓H␈↓␈↓ ∧7␈↓↓∀yy: [size ␈↓αa|␈↓↓yy<size ␈↓αa|␈↓↓xx ⊃ PAIR gopher yy]␈↓ 

␈↓ ↓H␈↓There␈α∞are␈α∂two␈α∞cases␈α∂corresponding␈α∞two␈α∂parts␈α∞of␈α∂the␈α∞definition␈α∂of␈α∞␈↓↓gopher.␈↓␈α∂ If␈α∞␈↓↓ATOM␈α∂␈↓αa|␈↓↓xx␈↓␈α∞then
␈↓ ↓H␈↓␈↓↓PAIR␈α
gopher␈α
xx␈↓␈α
follows␈α
from␈α
␈↓¬GOPHER-ATOM.␈α
␈↓␈α
If␈α
␈↓↓PAIR␈α
␈↓αa|␈↓↓xx␈↓␈α
then␈α
␈↓↓PAIR␈α
gopher␈α
xx␈↓␈α
follows␈α
from
␈↓ ↓H␈↓␈↓¬GOPHER-PAIR,␈α
␈↓␈α␈↓¬GOPHER-RANK␈α
␈↓and␈αthe␈α
induction␈α
hypothesis.␈α This␈α
completes␈αthe␈α
proof␈α
of␈α␈↓¬PAIR-
␈↓ ↓H␈↓¬GOPHER. ␈↓

␈↓ ↓H␈↓The proof of ␈↓↓∀xx: size gopher xx = size xx␈↓ is similar.  We need a lemma about ␈↓↓size:␈↓

␈↓ ↓H␈↓␈↓ β{␈↓↓∀xx: [PAIR ␈↓αa|␈↓↓xx⊃size xx=size [␈↓αaa|␈↓↓xx . [␈↓αda|␈↓↓xx  ␈↓αd|␈↓↓xx]]]␈↓ 

␈↓ ↓H␈↓which␈α⊃follows␈α⊃from␈α∩␈↓¬SIZE-PAIR␈α⊃␈↓and␈α⊃the␈α∩associativity␈α⊃of␈α⊃"+".␈α∩ We␈α⊃assume␈α⊃the␈α∩rank␈α⊃induction
␈↓ ↓H␈↓hypothesis

␈↓ ↓H␈↓␈↓ ∧∃␈↓↓∀yy: [size ␈↓αa|␈↓↓yy<size ␈↓αa|␈↓↓xx ⊃ size gopher yy=size yy]␈↓ 

␈↓ ↓H␈↓If␈α∃␈↓↓ATOM ␈↓αa|␈↓↓xx␈↓␈α∃then␈α∃␈↓↓size gopher xx=size xx␈↓␈α∃follows␈α∃from␈α∃␈↓¬GOPHER-ATOM.␈α∃␈↓␈α∃If␈α∃␈↓↓PAIR ␈↓αa|␈↓↓xx␈↓␈α∀then
␈↓ ↓H␈↓␈↓↓size gopher xx=size xx␈↓␈α∀follows␈α∀from␈α∀␈↓¬GOPHER-PAIR,␈α∃␈↓␈α∀␈↓¬GOPHER-RANK,␈α∀␈↓the␈α∀size␈α∀lemma␈α∃and␈α∀the
␈↓ ↓H␈↓induction hypothesis.  This completes the proof of ␈↓¬SIZE-GOPHER. ␈↓

␈↓ ↓H␈↓        We␈α∞have␈α∞presented␈α∂the␈α∞idea␈α∞of␈α∂a␈α∞rank␈α∞function␈α∞taking␈α∂only␈α∞one␈α∞argument␈α∂for␈α∞notational
␈↓ ↓H␈↓simplicity.␈α⊃  This␈α⊂idea␈α⊃and␈α⊃the␈α⊂corresponding␈α⊃induction␈α⊃principle␈α⊂generalize␈α⊃easily␈α⊃to␈α⊂multiple
␈↓ ↓H␈↓arguments.

␈↓ ↓H␈↓        To␈α⊃conclude␈α⊃our␈α⊃discussion␈α⊃of␈α⊃induction␈α⊃principles␈α⊃we␈α⊃will␈α⊃show␈α⊃how␈α⊃␈↓¬RANKIND␈α⊃␈↓can␈α⊂be
␈↓ ↓H␈↓derived␈αfrom␈α␈↓¬NUMBINDUCTION-CVI.␈α␈↓␈αIn␈αparticular␈αlet␈α␈↓↓D␈↓␈αbe␈αsome␈αdomain,␈α␈↓πx␈↓,␈α␈↓πx␈↓␈↓β1␈↓␈αvariables␈αranging
␈↓ ↓H␈↓over␈α∂␈↓↓D␈↓␈α⊂and␈α∂let␈α⊂␈↓πr␈↓␈α∂be␈α⊂a␈α∂rank␈α∂function␈α⊂␈↓↓␈↓πr␈↓↓: D → NATNUM␈↓␈α∂and␈α⊂let␈α∂␈↓πY␈↓␈α⊂be␈α∂a␈α⊂predicate␈α∂parameter.
␈↓ ↓H␈↓Then we want to prove:

␈↓ ↓H␈↓␈↓¬RANKIND: ␈↓    ␈↓↓∀␈↓πx␈↓↓: [∀␈↓πx␈↓↓␈↓β1␈↓↓:[␈↓πr␈↓↓ ␈↓πx␈↓↓␈↓β1␈↓↓ <␈↓πr␈↓↓ ␈↓πx␈↓↓ ⊃␈↓πY␈↓↓ ␈↓πx␈↓↓␈↓β1␈↓↓] ⊃ ␈↓πY␈↓↓ ␈↓πx␈↓↓] ⊃ ∀␈↓πx␈↓↓: ␈↓πY␈↓↓ ␈↓πx␈↓↓␈↓

␈↓ ↓H␈↓We instantiate ␈↓¬NUMBINDUCTION-CVI ␈↓taking ␈↓↓␈↓πF␈↓↓[n]≡∀␈↓πx␈↓↓: ␈↓πr␈↓↓ ␈↓πx␈↓↓=n ⊃ ␈↓πY␈↓↓ ␈↓πx␈↓↓␈↓ obtaining

␈↓ ↓H␈↓␈↓¬RANK-NUM: ␈↓    ␈↓↓∀n: [∀m: [m < n ⊃ ∀␈↓πx␈↓↓: [␈↓πr␈↓↓ ␈↓πx␈↓↓=m⊃␈↓πY␈↓↓ ␈↓πx␈↓↓]] ⊃ ∀␈↓πx␈↓↓: [␈↓πr␈↓↓ ␈↓πx␈↓↓=n⊃␈↓πY␈↓↓ ␈↓πx␈↓↓] ] ⊃ ∀n: ∀␈↓πx␈↓↓: [␈↓πr␈↓↓ ␈↓πx␈↓↓=n⊃␈↓πY␈↓↓ ␈↓πx␈↓↓]␈↓

␈↓ ↓H␈↓To prove ␈↓¬RANKIND ␈↓we assume

␈↓ ↓H␈↓␈↓¬RANKHYP: ␈↓    ␈↓↓∀␈↓πx␈↓↓: [∀␈↓πx␈↓↓␈↓β1␈↓↓:[␈↓πr␈↓↓ ␈↓πx␈↓↓␈↓β1␈↓↓ <␈↓πr␈↓↓ ␈↓πx␈↓↓ ⊃␈↓πY␈↓↓ ␈↓πx␈↓↓␈↓β1␈↓↓] ⊃ ␈↓πY␈↓↓ ␈↓πx␈↓↓] ␈↓

␈↓ ↓H␈↓and show ␈↓↓ ∀␈↓πx␈↓↓: ␈↓πY␈↓↓ ␈↓πx␈↓↓␈↓.  Since the range of ␈↓πr␈↓ is ␈↓↓NATNUM,␈↓ by ␈↓¬RANK-NUM ␈↓ it is sufficient to show

␈↓ ↓H␈↓␈↓ β;␈↓↓∀n: [∀m: [m < n ⊃ ∀␈↓πx␈↓↓: [␈↓πr␈↓↓ ␈↓πx␈↓↓=m⊃␈↓πY␈↓↓ ␈↓πx␈↓↓]] ⊃ ∀␈↓πx␈↓↓: [␈↓πr␈↓↓ ␈↓πx␈↓↓=n⊃␈↓πY␈↓↓ ␈↓πx␈↓↓] ].      ␈↓ 

␈↓ ↓H␈↓Thus we assume
␈↓ ↓H␈↓82␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓␈↓¬RNUM-HYP: ␈↓    ␈↓↓∀m: [m < n ⊃ ∀␈↓πx␈↓↓: [␈↓πr␈↓↓ ␈↓πx␈↓↓=m⊃␈↓πY␈↓↓ ␈↓πx␈↓↓]] ␈↓

␈↓ ↓H␈↓and prove ␈↓↓∀␈↓πx␈↓↓: [␈↓πr␈↓↓ ␈↓πx␈↓↓=n⊃␈↓πY␈↓↓ ␈↓πx␈↓↓] ]␈↓  Assuming

␈↓ ↓H␈↓␈↓¬RHO-N: ␈↓␈↓↓␈↓πr␈↓↓ ␈↓πx␈↓↓=n␈↓

␈↓ ↓H␈↓by ␈↓¬RANKHYP,RHO-N ␈↓it is sufficient to show ␈↓↓∀␈↓πx␈↓↓␈↓β1␈↓↓:[␈↓πr␈↓↓ ␈↓πx␈↓↓␈↓β1␈↓↓ < n ⊃␈↓πY␈↓↓ ␈↓πx␈↓↓␈↓β1␈↓↓].␈↓ Assuming

␈↓ ↓H␈↓␈↓¬RHO-M: ␈↓␈↓↓␈↓πr␈↓↓ ␈↓πx␈↓↓1 < n␈↓

␈↓ ↓H␈↓by ␈↓¬RNUM-HYP ␈↓with ␈↓↓m=␈↓πr␈↓↓ ␈↓πx␈↓↓␈↓β1␈↓↓␈↓  and ␈↓¬RHO-M ␈↓we have ␈↓↓␈↓πY␈↓↓ ␈↓πx␈↓↓␈↓β1␈↓↓␈↓ which completes the proof.

␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓        1. Prove the following additional properties of ␈↓↓gopher␈↓ using induction on the rank.

␈↓ ↓H␈↓␈↓¬CAR-GOPHER-FRINGE: ␈↓␈↓ ∧H␈↓↓∀xx: ␈↓αa|␈↓↓fringe xx = ␈↓αa|␈↓↓gopher xx␈↓
␈↓ ↓H␈↓␈↓¬CDR-GOHPER-FRINGE: ␈↓␈↓ ∧H␈↓↓∀xx: ␈↓αd|␈↓↓fringe xx = fringe ␈↓αd|␈↓↓gopher xx␈↓

␈↓ ↓H␈↓        2.␈αDerive␈αthe␈αschema␈α␈↓¬SEXPINDUCTTION␈α␈↓from␈αthe␈αschema␈α␈↓¬NUMBINDUCTION-CVI␈α␈↓␈αby␈αdevising
␈↓ ↓H␈↓a suitable rank function.

␈↓ ↓H␈↓[Remark:␈α the␈αpoint␈αof␈αthis␈αexercise␈αand␈αthe␈αabove␈αproof␈αis␈αto␈αgive␈αyou␈αthe␈αidea␈αthat␈αthe␈αvarious
␈↓ ↓H␈↓induction␈α⊂principles␈α⊂are␈α⊂just␈α⊂different␈α⊂ways␈α⊂of␈α⊂saying␈α⊂the␈α⊂same␈α⊂thing.␈α⊂ In␈α⊂a␈α⊂practical␈α⊂proving
␈↓ ↓H␈↓system it is useful to have a lot of redundancy an many ways to express various notions.]



␈↓ ↓H␈↓9.  ␈↓αPartial functions and the Minimization Schema.␈↓


␈↓ ↓H␈↓        So␈α⊃far␈α⊃we␈α⊃have␈α⊃dealt␈α⊃only␈α⊃with␈α⊃recursive␈α⊃programs␈α⊃that␈α⊃defined␈α⊃total␈α⊃functions␈α⊃on␈α⊃S-
␈↓ ↓H␈↓expressions.␈α⊃  In␈α⊃these␈α⊂cases␈α⊃the␈α⊃functional␈α⊃equation␈α⊂corresponding␈α⊃to␈α⊃the␈α⊃recursive␈α⊂definition
␈↓ ↓H␈↓completely␈α∃characterized␈α∃the␈α∃function␈α∃on␈α∀S-expressions.␈α∃ Frequently␈α∃we␈α∃are␈α∃interested␈α∃in␈α∀a
␈↓ ↓H␈↓program␈α
that␈αdoes␈α
not␈αalways␈α
terminate␈αand␈α
in␈αthis␈α
case␈αthere␈α
are␈αlikely␈α
to␈αbe␈α
many␈αfunctions␈α
that
␈↓ ↓H␈↓satisfy␈α∂the␈α∂corresponding␈α∂functional␈α∂equation.␈α∂ Whatever␈α∂we␈α∂prove␈α∂using␈α∂this␈α∂equation␈α∂will␈α∂be
␈↓ ↓H␈↓true␈αfor␈αall␈αof␈αthese␈αfunctions,␈αbut␈αthere␈α
are␈αalso␈αfacts␈αabout␈αthe␈αprogram␈αwe␈αare␈αrepresenting␈α
that
␈↓ ↓H␈↓will not be provable using only the functional equation.  For example, the program

␈↓ ↓H␈↓9.1)␈↓ ¬
␈↓↓loop x ← loop x                   ␈↓ 

␈↓ ↓H␈↓leads to the sentence

␈↓ ↓H␈↓9.2)␈↓ ¬␈↓↓∀x: [loop x = loop x]            ␈↓ 

␈↓ ↓H␈↓which␈αprovides␈αno␈αinformation␈α(all␈α
functions␈αsatisfy␈αit)␈αalthough␈αthe␈αfunction␈α
␈↓↓loop␈↓␈αcorresponding
␈↓ ↓H␈↓to the program is undefined for all ␈↓↓x.␈↓  This is not always the case.   Recall the program

␈↓ ↓H␈↓9.3)␈↓ ∧Q␈↓↓omega x ← [omega x] . ␈↓¬NIL␈↓↓               ␈↓ 

␈↓ ↓H␈↓which has the functional equation
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *83


␈↓ ↓H␈↓9.4)␈↓ ∧R␈↓↓∀x: [omega x = [omega x] . ␈↓¬NIL␈↓↓] .      ␈↓ 

␈↓ ↓H␈↓We␈α
showed␈α
in␈α
␈↓π∞␈↓␈α
6␈α∞that␈α
assuming␈α
␈↓↓SEXP␈α
omega␈α
x␈↓␈α
leads␈α∞to␈α
a␈α
contradiction␈α
thus␈α
we␈α
can␈α∞show␈α
␈↓↓∀x:
␈↓ ↓H␈↓↓[¬SEXP omega x]␈↓, using the functional equation and induction.

␈↓ ↓H␈↓        In␈α
order␈αto␈α
characterize␈α
recursive␈αprograms,␈α
we␈α
need␈αsome␈α
way␈α
of␈αexpressing␈α
the␈α
fact␈αthat
␈↓ ↓H␈↓the␈αfunction␈αwe␈α
mean␈αis␈αthe␈α
least␈αdefined␈αsolution␈αof␈α
the␈αfunctional␈αequation.␈α
  We␈αwill␈αdo␈αthis␈α
by
␈↓ ↓H␈↓introducing,␈α∩in␈α∩addition␈α∩to␈α∩the␈α⊃functional␈α∩equation,␈α∩a␈α∩schema␈α∩called␈α∩the␈α⊃␈↓↓minimization schema␈↓
␈↓ ↓H␈↓which␈α
expresses␈αthe␈α
fact␈αthat␈α
every␈α
function␈αsatisfying␈α
the␈αequation␈α
on␈α
the␈αdomain␈α
of␈αdefinition␈α
is
␈↓ ↓H␈↓defined␈αat␈αleast␈αevery␈αwhere␈αthat␈αthe␈αfunction␈αassigned␈αto␈αthe␈αprogram␈αis␈αdefined,␈αand␈αwhen␈αboth
␈↓ ↓H␈↓are defined, the have the same value.

␈↓ ↓H␈↓        If the program ␈↓↓f␈↓ is define by

␈↓ ↓H␈↓9.5) ␈↓ ¬C␈↓↓f ␈↓πx␈↓↓ ← ␈↓πt␈↓↓[f,␈↓πx␈↓↓]           ␈↓ 

␈↓ ↓H␈↓we have a function ␈↓↓f␈↓ satisyfying the functional equation

␈↓ ↓H␈↓9.6) ␈↓ ¬#␈↓↓∀␈↓πx␈↓↓: f ␈↓πx␈↓↓ = ␈↓πt␈↓↓[f,␈↓πx␈↓↓]             ␈↓ 

␈↓ ↓H␈↓where␈α∞␈↓πx␈↓␈α
is␈α∞a␈α∞variable␈α
ranging␈α∞over␈α∞the␈α
domain␈α∞␈↓↓D.␈↓␈α
 The␈α∞minimization␈α∞schema␈α
for␈α∞this␈α∞form␈α
of
␈↓ ↓H␈↓recursive definition has the form

␈↓ ↓H␈↓9.7)␈↓ βT␈↓↓∀␈↓πx␈↓↓: [D ␈↓πt␈↓↓[F,␈↓πx␈↓↓] ⊃ F ␈↓πx␈↓↓ = ␈↓πt␈↓↓[F,␈↓πx␈↓↓]] ⊃ ∀␈↓πx␈↓↓: [D f ␈↓πx␈↓↓ ⊃ f ␈↓πx␈↓↓ = F ␈↓πx␈↓↓] ␈↓. 

␈↓ ↓H␈↓Here␈α
␈↓↓F␈↓␈α
is␈αa␈α
function␈α
parameter.␈α
  This␈αschema␈α
is␈α
really␈α␈↓↓schema schema,␈↓␈α
since␈α
for␈α
any␈αparticular
␈↓ ↓H␈↓term␈α⊂␈↓πt␈↓␈α⊂and␈α⊂function␈α⊂symbol␈α⊂␈↓↓f␈↓␈α⊂we␈α⊂produce␈α⊂from␈α⊂the␈α⊂schema␈α⊂a␈α⊂formula␈α⊂that␈α⊂still␈α⊃contains␈α⊂the
␈↓ ↓H␈↓paramater␈α∩␈↓↓F.␈↓␈α⊃ We␈α∩then␈α⊃must␈α∩substitute␈α⊃some␈α∩function␈α⊃expression␈α∩of␈α⊃our␈α∩language␈α⊃for␈α∩␈↓↓F␈↓␈α⊃to
␈↓ ↓H␈↓produce␈α
an␈α
axiom.␈α
 (This␈αis␈α
similar␈α
to␈α
the␈α
induction␈αschemas,␈α
except␈α
there␈α
we␈αsubstituted␈α
formulas
␈↓ ↓H␈↓for predicate parameters.)

␈↓ ↓H␈↓        The␈αsimplest␈α
application␈αof␈αthe␈α
minimization␈αschema␈αis␈α
to␈αshow␈αthat␈α
the␈αprogram␈α
for␈α␈↓↓loop␈↓
␈↓ ↓H␈↓given above computes the totally undefined function.  The minimization schema for loop is

␈↓ ↓H␈↓9.8)␈↓ β≡␈↓↓∀x:[SEXP F x ⊃ F x = F x] ⊃ ∀x: [SEXP loop x ⊃ loop x = F x] ␈↓. 

␈↓ ↓H␈↓If we let ␈↓↓F␈↓ be the function expression λX: ␈↓π|␈↓ then we obtain the axiom

␈↓ ↓H␈↓␈↓ βT␈↓↓∀x:[SEXP ␈↓π|␈↓↓ ⊃ ␈↓π|␈↓↓ = ␈↓π|␈↓↓] ⊃ ∀x: [SEXP loop x ⊃ loop x = ␈↓π|␈↓↓] ␈↓. 

␈↓ ↓H␈↓The␈α∞left␈α
side␈α∞of␈α
the␈α∞implication␈α
is␈α∞identically␈α
true␈α∞since␈α
␈↓↓SEXP␈α∞∩␈α
BOTTOM␈α∞=␈α
␈↓πf␈↓↓␈↓␈α∞ or␈α
␈↓↓¬SEXP ␈↓π|␈↓↓␈↓.
␈↓ ↓H␈↓Thus we have

␈↓ ↓H␈↓␈↓ ∧u␈↓↓∀x: [SEXP loop x ⊃ loop x = ␈↓π|␈↓↓] ␈↓. 

␈↓ ↓H␈↓If we assume ␈↓↓SEXP loop x␈↓ we obtain SEXP ␈↓π|␈↓ which is a contradiction.  Thus we have shown

␈↓ ↓H␈↓␈↓ ¬↓␈↓↓∀x: ¬SEXP loop x                ␈↓ 

␈↓ ↓H␈↓as desired.
␈↓ ↓H␈↓84␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓        The␈αminimization␈αschema␈αcan␈αsometimes␈αbe␈αused␈αto␈αshow␈αpartial␈αcorrectness.␈α For␈αexample,
␈↓ ↓H␈↓the well known 91-function is defined by the recursive program over the natural numbers

␈↓ ↓H␈↓␈↓ ∧]␈↓↓f␈↓β91␈↓↓ n ← ␈↓πt␈↓↓␈↓β91␈↓↓[f␈↓β91␈↓↓,n]                       ␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ βh␈↓↓␈↓πt␈↓↓␈↓β91␈↓↓[F,n] = ␈↓αif␈↓↓ n > 100 ␈↓αthen␈↓↓ n - 10 ␈↓αelse␈↓↓ F F [n + 11] ␈↓.

␈↓ ↓H␈↓The corresponding minimization schema is

␈↓ ↓H␈↓␈↓ α∪␈↓↓∀n: [NATNUM ␈↓πt␈↓↓␈↓β91␈↓↓[F,n] ⊃ F n = ␈↓πt␈↓↓␈↓β91␈↓↓[F,n]] ⊃  ∀n: [NATNUM f␈↓β91␈↓↓ n ⊃ f␈↓β91␈↓↓ n = F n] ␈↓. 

␈↓ ↓H␈↓The goal is to show that

␈↓ ↓H␈↓␈↓ ∧A␈↓↓∀n: [f␈↓β91␈↓↓ n = F␈↓β91␈↓↓ n                             ␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓ ∧∀␈↓↓∀n: [F␈↓β91␈↓↓ n = ␈↓αif␈↓↓ n > 100 ␈↓αthen␈↓↓ n - 10 ␈↓αelse␈↓↓ 91] .  ␈↓

␈↓ ↓H␈↓Using the minization schema we can easily show that

␈↓ ↓H␈↓␈↓ ∧_␈↓↓∀n: [NATNUM f␈↓β91␈↓↓ n ⊃ f␈↓β91␈↓↓ n = F␈↓β91␈↓↓ n .         ␈↓ 

␈↓ ↓H␈↓Namely, we need only show

␈↓ ↓H␈↓␈↓ βv␈↓↓∀n: [NATNUM ␈↓πt␈↓↓␈↓β91␈↓↓[F␈↓β91␈↓↓,n] ⊃ F␈↓β91␈↓↓ n = ␈↓πt␈↓↓␈↓β91␈↓↓[F␈↓β91␈↓↓,n]] ␈↓ 

␈↓ ↓H␈↓which␈αfollows␈αby␈α
direct␈αcalculation␈αfrom␈αthe␈α
definitions␈αof␈α␈↓↓F␈↓β91␈↓↓␈↓␈αand␈α
␈↓↓␈↓πt␈↓↓␈↓β91␈↓↓␈↓␈αby␈αconsidering␈α
the␈αthree
␈↓ ↓H␈↓cases ␈↓↓n>100,␈↓ ␈↓↓89<n≤100␈↓ and ␈↓↓n≤89.␈↓  The hypothesis of the implication is not needed here.

␈↓ ↓H␈↓        The␈α∂method␈α∂of␈α∂␈↓↓recursion␈↓␈α∂␈↓↓induction␈↓␈α∂is␈α∂also␈α∂an␈α∂immediate␈α∂application␈α∂of␈α∂the␈α∞minimization
␈↓ ↓H␈↓schema.␈α
 If␈αwe␈α
show␈αthat␈α
two␈αfunctions␈α
satisfy␈αthe␈α
schema␈αof␈α
a␈αrecursive␈α
program,␈αwe␈α
show␈αthat
␈↓ ↓H␈↓they both equal the function computed by the program wherever the function is defined.

␈↓ ↓H␈↓For example we can use the minimization schema for the program

␈↓ ↓H␈↓␈↓¬REV: ␈↓  ␈↓↓rev[u,v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ qv ␈↓αelse␈↓↓ rev[␈↓αd|␈↓↓u,␈↓αa|␈↓↓u .  v]␈↓

␈↓ ↓H␈↓and the fact that ␈↓↓∀u v: LIST rev[u,v]␈↓ to show that

␈↓ ↓H␈↓␈↓¬EQ-REV:␈α∞␈↓␈↓↓∀u: rev[u,␈↓¬NIL␈↓↓]=reverse[u]␈α∞where␈α∂ ␈↓¬REVERSE:␈α∞␈↓↓  ␈↓␈↓↓reverse␈↓␈α∞u␈α∞←␈α∂␈↓αif␈↓␈α∞␈↓αn|␈↓u␈α∞␈↓αthen␈↓␈α∞␈↓¬NIL␈↓␈α∂␈↓αelse␈↓␈α∞[reverse
␈↓ ↓H␈↓␈↓αd|␈↓u]*<␈↓αa|␈↓u>␈↓↓.  The␈↓ minimization schema for the program ␈↓↓rev␈↓ is

␈↓ ↓H␈↓␈↓ ↓Q␈↓↓∀u v: [LIST ␈↓πt␈↓↓␈↓βrev␈↓↓[F,u,v] ⊃ F[u,v] = ␈↓πt␈↓↓␈↓βrev␈↓↓[F,u,v]] ⊃  ∀u v: [LIST rev[u,v] ⊃ rev[u,v] = F[u,v]] ␈↓. 

␈↓ ↓H␈↓Taking␈α#␈↓↓F[u,v]=revrse[u]*v␈↓,␈α$we␈α#need␈α#only␈α$show␈α#␈↓↓F[u,v]=␈↓πt␈↓↓␈↓βrev␈↓↓[F,u,v]␈↓␈α#as␈α$this␈α#gives
␈↓ ↓H␈↓␈↓↓∀u v: rev[u,v]=reverse[u]*v␈↓ and ␈↓¬EQ-REV ␈↓is a special case.  By the definitions we need only prove

␈↓ ↓H␈↓␈↓ βx␈↓↓reverse[u]*v=␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ reverse[␈↓αd|␈↓↓u]*[␈↓αa|␈↓↓u . v]␈↓. 
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *85


␈↓ ↓H␈↓In␈α
the␈αcase␈α
␈↓↓u␈↓␈α
is␈α␈↓¬NIL␈↓␈α
both␈αsides␈α
of␈α
the␈αequation␈α
simplify␈α
to␈α␈↓↓v.␈↓␈α
 In␈αthe␈α
case␈α
␈↓↓u␈↓␈αis␈α
some␈αnon-empty␈α
list
␈↓ ↓H␈↓␈↓↓uu␈↓ we by ␈↓¬REVERSE ␈↓we need only show

␈↓ ↓H␈↓␈↓ ∧β␈↓↓ [reverse[␈↓αd|␈↓↓uu]*<␈↓αa|␈↓↓uu>]*v=reverse[␈↓αd|␈↓↓uu]*[␈↓αa|␈↓↓uu . v].␈↓ 

␈↓ ↓H␈↓But this is an easy consequence of ␈↓¬ASSOC-APPEND,APPEND-NIL,APPEND-UU. ␈↓

␈↓ ↓H␈↓        The␈α
utility␈α
of␈α
the␈α
minimization␈αschema␈α
for␈α
proving␈α
partial␈α
correctness␈α
or␈αnon-termination
␈↓ ↓H␈↓depends␈α
on␈α
our␈α
ability␈α
to␈α
name␈α∞suitable␈α
comparison␈α
functions.␈α
 ␈↓↓loop␈↓␈α
and␈α
␈↓↓f␈↓β91␈↓↓␈↓␈α
were␈α∞easily␈α
treated,
␈↓ ↓H␈↓because␈α⊃the␈α⊃necessary␈α⊃comparison␈α⊃functions␈α⊃could␈α⊃be␈α⊃given␈α⊃explicitly␈α⊃without␈α⊃recursion.␈α⊂ Any
␈↓ ↓H␈↓extension␈αof␈αthe␈αlanguage␈αthat␈αprovides␈αnew␈αtools␈αfor␈αnaming␈αcomparison␈αfunctions,␈αe.g.␈αgoing␈αto
␈↓ ↓H␈↓higher order logic, will improve our ability to use the schema in proofs.



␈↓ ↓H␈↓10.  ␈↓αTheory of LISP: Algebraic Axioms.␈↓


␈↓ ↓H␈↓         In␈α∂this␈α∞appendix␈α∂we␈α∞collect␈α∂the␈α∞language␈α∂specification,␈α∞domain␈α∂facts␈α∞and␈α∂axioms␈α∂for␈α∞the
␈↓ ↓H␈↓theory␈α∞of␈α∞extended␈α∞S-expressions␈α∞and␈α∞the␈α
subdomains␈α∞of␈α∞S-expressions,␈α∞lists␈α∞and␈α∞numbers,␈α
and
␈↓ ↓H␈↓the definitions of functions representing the basic LISP programs.


␈↓ ↓H␈↓αDomains

␈↓ ↓H␈↓␈↓↓ESEXP,␈↓␈α∀␈↓↓BOTTOM,␈↓␈α∀␈↓↓SEXP,␈↓␈α∀␈↓↓PAIR,␈↓␈α∀␈↓↓ATOM,␈↓␈α∀␈↓↓LIST,␈↓␈α∀␈↓↓NELIST,␈↓␈α∀and␈α∀␈↓↓NULL␈↓␈α∀␈↓↓NATNUM,␈↓
␈↓ ↓H␈↓␈↓↓POSP,␈↓ and ␈↓↓ZERO.␈↓


␈↓ ↓H␈↓αDomain relations:

␈↓ ↓H␈↓␈↓ βx␈↓↓ESEXP = SEXP ∪ BOTTOM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓SEXP ∩ BOTTOM = ␈↓πf␈↓↓␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓SEXP ⊂ ESEXP␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓SEXP = ATOM ∪ PAIR␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ATOM ∩ PAIR = ␈↓πf␈↓↓␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓LIST ⊂ SEXP␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓NELIST ⊂ PAIR␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓NULL ≡ {␈↓¬NIL␈↓↓}␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓LIST = NULL ∪ LIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓NULL ∩ LIST = ␈↓πf␈↓↓␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓NATNUM ⊂ ATOM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ZERO ≡ {␈↓¬0␈↓↓}␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓NATNUM = ZER0 ∪ POSP␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ZERO ∩ POSP = ␈↓πf␈↓↓␈↓
␈↓ ↓H␈↓86␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓αConstants:

␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓¬T␈↓↓ ε ATOM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓¬NIL␈↓↓ ε NULL␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓π|␈↓↓ ε BOTTOM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓¬0␈↓↓ ε ZERO␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓¬1,2,...␈↓↓ ε POSP␈↓


␈↓ ↓H␈↓αVariables:

␈↓ ↓H␈↓␈↓ βx␈↓↓X, Y, Z, U, V, W, M, N ε ESEXP␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓x, y, z ε SEXP␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓xx, yy, zz ε PAIR␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓a, b, c ε ATOM␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓u, v, w ε LIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓uu, vv, ww ε NELIST␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓l, n, m ε NATNUM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓ll, mm, nn ε POSP␈↓

␈↓ ↓H␈↓[Remark:␈α
If␈α
␈↓πx␈↓␈α
is␈α
a␈α
variable␈α
ranging␈α
over␈α
domain␈α
␈↓↓D␈↓␈α
then␈α
we␈α
may␈α
also␈α
use␈α
␈↓πx␈↓␈↓β0␈↓,␈α
␈↓πx␈↓␈↓β1␈↓,␈α
...␈α
as␈α
variables
␈↓ ↓H␈↓ranging over ␈↓↓D␈↓].


␈↓ ↓H␈↓αFunction symbols and domain mappings:

␈↓ ↓H␈↓The basic function symbols are: ␈↓αa␈↓, ␈↓αd␈↓, ␈↓αcons␈↓, ␈↓↓add1,␈↓ ␈↓↓sub1,␈↓ ␈↓αat␈↓, ␈↓αn␈↓, ␈↓αif␈↓, ␈↓αnot␈↓, ␈↓αand␈↓, ␈↓αor␈↓.

␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αa␈↓↓: BOTTOM → BOTTOM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αa␈↓↓: PAIR → SEXP␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αa␈↓↓: NELIST → SEXP␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αd␈↓↓: BOTTOM → BOTTOM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αd␈↓↓: PAIR → SEXP␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αd␈↓↓: NELIST → LIST␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αcons␈↓↓: BOTTOM ␈↓¬x␈↓↓ ESEXP → BOTTOM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αcons␈↓↓: ESEXP ␈↓¬x␈↓↓ BOTTOM → BOTTOM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αcons␈↓↓: SEXP ␈↓¬x␈↓↓ SEXP → PAIR␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αcons␈↓↓: SEXP ␈↓¬x␈↓↓ LIST → NELIST␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓add1: BOTTOM → BOTTOM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓add1: NATNUM → POSP␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓sub1: BOTTOM → BOTTOM␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓sub1: POSP → NATNUM␈↓
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *87


␈↓ ↓H␈↓αAlgebraic Axioms

␈↓ ↓H␈↓␈↓ βx␈↓¬CAR:␈↓  ␈↓↓∀x y.␈↓αa|␈↓↓[x . y] = x␈↓
␈↓ ↓H␈↓␈↓ βx␈↓¬CDR:␈↓  ␈↓↓∀x y.␈↓αd|␈↓↓[x . y] = y␈↓
␈↓ ↓H␈↓␈↓ βx␈↓¬CONS:␈↓  ␈↓↓∀xx.[␈↓αa|␈↓↓xx . ␈↓αd|␈↓↓xx] = xx␈↓
␈↓ ↓H␈↓␈↓ βx␈↓¬EQPAIR:␈↓  ␈↓↓∀xx yy.[[␈↓αa|␈↓↓xx = ␈↓αa|␈↓↓yy] ∧ [␈↓αd|␈↓↓xx = ␈↓αd|␈↓↓yy] ≡ xx = yy]␈↓

␈↓ ↓H␈↓␈↓ βx␈↓¬SUB1:␈↓  ␈↓↓∀n: sub1 add1 n = n␈↓
␈↓ ↓H␈↓␈↓ βx␈↓¬ADD1:␈↓  ␈↓↓∀nn: add1 sub1 nn = nn␈↓


␈↓ ↓H␈↓αInduction schemata

␈↓ ↓H␈↓␈↓¬SEXPINDUCTION: ␈↓␈↓ ∧λ␈↓↓∀a:␈↓πF␈↓↓ a ∧ ∀xx:[␈↓πF␈↓↓ ␈↓αa|␈↓↓xx ∧ ␈↓πF␈↓↓ ␈↓αd|␈↓↓xx ⊃ ␈↓πF␈↓↓ xx] ⊃ ∀x: ␈↓πF␈↓↓ x ␈↓.
␈↓ ↓H␈↓␈↓¬LISTINDUCTION: ␈↓␈↓ ∧8␈↓↓␈↓πF␈↓↓ ␈↓¬NIL␈↓↓ ∧ ∀uu: [␈↓πF␈↓↓ ␈↓αd|␈↓↓uu ⊃ ␈↓πF␈↓↓ uu] ⊃ ∀u: ␈↓πF␈↓↓ u ␈↓.
␈↓ ↓H␈↓␈↓¬NUMINDUCTION: ␈↓␈↓ ∧0␈↓↓␈↓πF␈↓↓ ␈↓¬0 ␈↓↓∧ ∀nn: [␈↓πF␈↓↓ sub1 nn ⊃ ␈↓πF␈↓↓ nn] ⊃ ∀n: ␈↓πF␈↓↓ n.␈↓


␈↓ ↓H␈↓αDefining axioms for program primitives

␈↓ ↓H␈↓␈↓¬IF: ␈↓␈↓ α(␈↓↓∀X Y Z: ␈↓αif␈↓↓[X,Y,Z]=IF ¬SEXP X THEN ␈↓π|␈↓↓ ELSE IF X ≠ ␈↓¬NIL␈↓↓ THEN Y ELSE Z␈↓
␈↓ ↓H␈↓␈↓ α(␈↓↓∀x y z: ␈↓αif␈↓↓[x,y,z] = IF x ≠ ␈↓¬NIL␈↓↓ THEN y ELSE z␈↓
␈↓ ↓H␈↓␈↓ α(␈↓↓␈↓αif␈↓↓: SEXP ␈↓¬x␈↓↓ SEXP ␈↓¬x␈↓↓ SEXP → SEXP␈↓

␈↓ ↓H␈↓␈↓¬EQUAL: ␈↓␈↓ α(␈↓↓∀X Y: X equal Y=IF ¬[SEXP X∧SEXP Y] THEN ␈↓π|␈↓↓ ELSE IF X=Y THEN ␈↓¬T␈↓↓ ELSE ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α(␈↓↓∀x y: [x ␈↓αequal␈↓↓ y] = IF [x = y] THEN ␈↓¬T␈↓↓ ELSE ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α(␈↓↓␈↓αequal␈↓↓: SEXP ␈↓¬x␈↓↓ SEXP → SEXP␈↓

␈↓ ↓H␈↓␈↓¬ATOM: ␈↓␈↓ α(␈↓↓∀X: ␈↓αat|␈↓↓X=IF ¬SEXP X THEN ␈↓π|␈↓↓ ELSE IF ATOM X THEN ␈↓¬T␈↓↓ ELSE ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α(␈↓↓∀x: ␈↓αat|␈↓↓x = IF ATOM x THEN ␈↓¬T␈↓↓ ELSE ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α(␈↓↓␈↓αat␈↓↓: SEXP → SEXP␈↓

␈↓ ↓H␈↓␈↓¬NULL: ␈↓␈↓ α(␈↓↓∀X: ␈↓αn|␈↓↓X=IF ¬SEXP X THEN ␈↓π|␈↓↓ ELSE IF NULL X THEN ␈↓¬T␈↓↓ ELSE ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α(␈↓↓∀X: ␈↓αn|␈↓↓x = IF NULL x THEN ␈↓¬T␈↓↓ ELSE ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α(␈↓↓␈↓αn␈↓↓: SEXP → SEXP␈↓

␈↓ ↓H␈↓␈↓¬NOT: ␈↓␈↓ α(␈↓↓∀X: ␈↓αnot␈↓↓ X = ␈↓αif␈↓↓[X, ␈↓¬NIL␈↓↓, ␈↓¬T␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α(␈↓↓∀x: ␈↓αnot␈↓↓ x = ␈↓αif␈↓↓ x ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓¬T␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α(␈↓↓␈↓αn␈↓↓ot: SEXP → SEXP␈↓

␈↓ ↓H␈↓␈↓¬AND: ␈↓␈↓ α(␈↓↓∀X Y: X ␈↓αand␈↓↓ Y = ␈↓αif␈↓↓[X,Y,X]␈↓
␈↓ ↓H␈↓␈↓ α(␈↓↓∀x y: x ␈↓αand␈↓↓ y = ␈↓αif␈↓↓ x ␈↓αthen␈↓↓ y ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α(␈↓↓␈↓αand␈↓↓: SEXP ␈↓¬x␈↓↓ SEXP → SEXP␈↓

␈↓ ↓H␈↓␈↓¬OR: ␈↓␈↓ α(␈↓↓∀X Y: X ␈↓αor␈↓↓ Y = ␈↓αif␈↓↓[X, X, Y]␈↓
␈↓ ↓H␈↓␈↓ α(␈↓↓∀x y: x or y = ␈↓αif␈↓↓ x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ y␈↓
␈↓ ↓H␈↓␈↓ α(␈↓↓␈↓αor␈↓↓: SEXP ␈↓¬x␈↓↓ SEXP → SEXP␈↓
␈↓ ↓H␈↓88␈↓ ¬xChapter  III␈↓ H


␈↓ ↓H␈↓11.  ␈↓αExercises ␈↓


␈↓ ↓H␈↓        The␈α∃following␈α∀are␈α∃properties␈α∀to␈α∃prove␈α∃about␈α∀programs␈α∃that␈α∀you␈α∃have␈α∃written.␈α∀ The
␈↓ ↓H␈↓functions␈αand␈αpredicates␈αreferred␈αto␈αare␈αdescribed␈αin␈αthe␈αExercises␈αof␈αChapter␈αII␈α␈↓π∞␈↓8.␈α  You␈αshould
␈↓ ↓H␈↓carry␈αout␈α
the␈αproofs␈αfor␈α
your␈αdefinitions␈α
of␈αthe␈αindicated␈α
functions␈αor␈α
predicates.␈α You␈αmay␈α
decide
␈↓ ↓H␈↓to␈α
rewrite␈α
your␈α
definition␈αbecause␈α
(1)␈α
you␈α
find␈αthat␈α
your␈α
function␈α
definition␈αis␈α
not␈α
correct␈α
or␈α(2)
␈↓ ↓H␈↓another␈α
definition␈α
is␈αcleaner␈α
and␈α
more␈αamenable␈α
to␈α
proofs.␈α This␈α
is␈α
quite␈αnatural␈α
if␈α
you␈α
are␈αnot
␈↓ ↓H␈↓thinking in terms of proving it correct when you write the definition.

␈↓ ↓H␈↓1. Lists

␈↓ ↓H␈↓        1.1. ␈↓↓∀u. samelength[u,reverse u]␈↓

␈↓ ↓H␈↓        1.2. ␈↓↓∀u v:istail[commontail[u,v],v]␈↓
␈↓ ↓H␈↓        1.3. ␈↓↓∀u v:istail[commontail[u,v],u]␈↓

␈↓ ↓H␈↓        1.4. ␈↓↓∀u v: commontail[u,v]=commontail[v,u]␈↓

␈↓ ↓H␈↓        1.5. ␈↓↓∀u v:[append[upto[commontail[u,v],v],commontail[u,v]]=v]␈↓
␈↓ ↓H␈↓        1.6. ␈↓↓∀u v:[append[upto[commontail[u,v],u],commontail[u,v]]=u]␈↓

␈↓ ↓H␈↓Note that ␈↓↓istail␈↓ has already been worked on in earlier exercises.

␈↓ ↓H␈↓2. Sexpressions

␈↓ ↓H␈↓        2.1. ␈↓↓∀x y:[x = get[y,point[x,y]]]␈↓

␈↓ ↓H␈↓        2.2. ␈↓↓∀x:[balanced[balance[x]] samefringe[balance[x],x]]␈↓

␈↓ ↓H␈↓3. Algebra and arithmetic

␈↓ ↓H␈↓        3.1. ␈↓↓∀u v: poly u ∧ poly v ⊃ sum[prod[quot[u,v],v],rem[u,v]␈↓

␈↓ ↓H␈↓        3.2. ␈↓↓∀x: arith x ⊃ numval[sop x]]=numval[x]␈↓

␈↓ ↓H␈↓        Part␈α∞of␈α∞the␈α∞exercise␈α∞is␈α∞to␈α∞invent␈α∞suitable␈α∞predicates␈α∞␈↓↓poly␈↓␈α∞and␈α∞␈↓↓arith␈α∞which␈↓␈α∂characterize␈α∞the
␈↓ ↓H␈↓domain on which the functions are valid.

␈↓ ↓H␈↓4. Sets

␈↓ ↓H␈↓        4.1. ␈↓↓∀x u v:[xε[u∪v] ⊃ xεu ∨ xεv]␈↓

␈↓ ↓H␈↓        4.2. ␈↓↓∀x u v:[[xεu] ⊃ ¬xε[v - u]]␈↓

␈↓ ↓H␈↓5. Permutations

␈↓ ↓H␈↓        5.1. ␈↓↓∀u:[lcycle rcycle u = u]␈↓

␈↓ ↓H␈↓        5.2. ␈↓↓∀u:[isperm1 u ⊃ [compose1[p,invert1[p]] = id1␈↓
␈↓ ↓H␈↓␈↓ ¬xChapter  III␈↓ *89


␈↓ ↓H␈↓        5.3. ␈↓↓∀u:[isperm1 u ⊃ [compose1[invert1[p],p] = id1␈↓

␈↓ ↓H␈↓        5.4. ␈↓↓u: [isperm1 u ⊃ [invert1 invert1 p = p]]␈↓

␈↓ ↓H␈↓        5.5. ␈↓↓∀u v:[isperm2 u ∧ isperm2 v ⊃ rho21[compose2[u,v]] = compose1[rho21 u,rho21 v2]]␈↓
␈↓ ↓H␈↓90␈↓ εH␈↓ H


␈↓ ↓H␈↓α␈↓ ¬{Chapter IV

␈↓ ↓H␈↓α␈↓ ¬.PROOFS: EXAMPLES


␈↓ ↓H␈↓        In␈αthis␈α
section␈αwe␈αpresent␈α
some␈αnon-trivial␈αexamples␈α
illustrating␈αthe␈αmethods␈α
developed␈αin
␈↓ ↓H␈↓Chapter III.



␈↓ ↓H␈↓1.  ␈↓αThe SAMEFRINGE problem.␈↓


␈↓ ↓H␈↓         As␈α∂a␈α∂non␈α∂trivial␈α∂application␈α∂of␈α∂the␈α∂techniques␈α∂described␈α∂in␈α∂Chapter␈α∂III,␈α∂we␈α∂will␈α⊂give␈α∂a
␈↓ ↓H␈↓proof␈α∂of␈α∞correctness␈α∂of␈α∞the␈α∂predicate␈α∞␈↓↓samefringe␈↓␈α∂which␈α∞has␈α∂been␈α∞proposed␈α∂as␈α∞a␈α∂solution␈α∂of␈α∞the
␈↓ ↓H␈↓SAMEFRINGE␈αproblem.␈α This␈α
is␈αthe␈αproblem␈αof␈α
determining␈αwhether␈αor␈αnot␈α
two␈αS-expressions
␈↓ ↓H␈↓have␈αthe␈αsame␈αfringe␈αusing␈αa␈αminimum␈αof␈αspace.␈α We␈αwill␈αbe␈αconcerned␈αonly␈αwith␈αthe␈αcorrectness
␈↓ ↓H␈↓of ␈↓↓samefringe␈↓ since the efficiency is not an ␈↓↓extensional␈↓ property.

␈↓ ↓H␈↓        We␈α∩have␈α∪already␈α∩studied␈α∩two␈α∪programs␈α∩for␈α∩computing␈α∪the␈α∩fringe␈α∩of␈α∪an␈α∩S-expression,
␈↓ ↓H␈↓namely␈α␈↓↓fringe␈↓␈αand␈α␈↓↓flatten,␈↓␈αwhich␈αwere␈αproved␈αto␈αcompute␈αthe␈αsame␈αfunction␈αfrom␈αS-expressions
␈↓ ↓H␈↓to non-empty lists.  The predicate ␈↓↓samefringe␈↓ is computed by the LISP program

␈↓ ↓H␈↓␈↓¬SAMEFR: ␈↓␈↓ β(␈↓↓samefringe[x, y] ← ␈↓
␈↓ ↓H␈↓␈↓ βh␈↓↓x ␈↓αequal␈↓↓ y ␈↓αor␈↓↓ [␈↓αnot␈↓↓ ␈↓αat|␈↓↓x ␈↓αand␈↓↓ ␈↓αnot␈↓↓ ␈↓αat|␈↓↓y ␈↓αand␈↓↓ same[gopher x, gopher y]]␈↓
␈↓ ↓H␈↓where
␈↓ ↓H␈↓␈↓¬SAME: ␈↓␈↓ β(␈↓↓same[x, y] ← ␈↓αa|␈↓↓x ␈↓αequal␈↓↓ ␈↓αa|␈↓↓y ␈↓αand␈↓↓ samefringe[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓¬GOPHER: ␈↓␈↓ β(␈↓↓gopher x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ gopher [␈↓αaa|␈↓↓x . [␈↓αda|␈↓↓x . ␈↓αd|␈↓↓x]]␈↓.

␈↓ ↓H␈↓What␈α∞we␈α∞want␈α
to␈α∞show␈α∞is␈α∞that␈α
␈↓↓samefringe␈↓␈α∞says␈α∞"yes"␈α∞for␈α
input␈α∞␈↓↓x,y␈↓␈α∞just␈α∞when␈α
␈↓↓fringe x=fringe y␈↓.
␈↓ ↓H␈↓To␈α⊃do␈α⊃this␈α∩we␈α⊃use␈α⊃the␈α∩ideas␈α⊃of␈α⊃Chapter␈α⊃III␈α∩␈↓π∞␈↓7.␈α⊃ We␈α⊃first␈α∩represent␈α⊃the␈α⊃above␈α∩programs␈α⊃by
␈↓ ↓H␈↓functions ␈↓↓samefringef,␈↓ ␈↓↓samef,␈↓ and ␈↓↓gopher␈↓ satisfying the axioms:

␈↓ ↓H␈↓␈↓¬SAMEFR-DEF: ␈↓␈↓ β(␈↓↓∀x y: samefringef[x, y] = ␈↓
␈↓ ↓H␈↓␈↓ ∧(␈↓↓x ␈↓αequal␈↓↓ y ␈↓αor␈↓↓ [␈↓αnot␈↓↓ ␈↓αat|␈↓↓x ␈↓αand␈↓↓ ␈↓αnot␈↓↓ ␈↓αat|␈↓↓y ␈↓αand␈↓↓ same[gopher x, gopher y]]␈↓
␈↓ ↓H␈↓␈↓¬SAME-DEF: ␈↓␈↓ β(␈↓↓∀x y: same[x, y] = ␈↓αa|␈↓↓x ␈↓αequal␈↓↓ ␈↓αa|␈↓↓y ␈↓αand␈↓↓ samefringef[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y]␈↓
␈↓ ↓H␈↓␈↓¬GOPHER-DEF: ␈↓␈↓ β(␈↓↓∀x: gopher x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ gopher [␈↓αaa|␈↓↓x . [␈↓αda|␈↓↓x . ␈↓αd|␈↓↓x]]␈↓.

␈↓ ↓H␈↓We then define the predicate ␈↓↓samefringe␈↓ by

␈↓ ↓H␈↓␈↓¬SAMEFRINGE-DEF ␈↓   ␈↓↓∀x y: [samefringe[x,y] ≡ True samefringef[x,y]]␈↓

␈↓ ↓H␈↓and the statement of correctness becomes

␈↓ ↓H␈↓␈↓¬SAMEFRINGE-COR: ␈↓  ␈↓↓∀x y: [samefringe[x,y] ≡ fringe x=fringe y].␈↓

␈↓ ↓H␈↓The␈α∂program␈α∂computing␈α∂␈↓↓samefringe␈↓␈α∂divides␈α∂pairs␈α∞␈↓↓x,y␈↓␈α∂of␈α∂S-expressions␈α∂into␈α∂two␈α∂cases:␈α∞␈↓¬ATOMXY
␈↓ ↓H␈↓¬␈↓where␈α
␈↓↓ATOM x ∨ ATOM y␈↓␈αand␈α
␈↓¬PAIRXY␈α
␈↓where␈α␈↓↓PAIR x ∧ PAIR y␈↓.␈α
 And␈αwe␈α
have␈α
two␈αimmediate
␈↓ ↓H␈↓corollaries of ␈↓¬SAMEFR-DEF ␈↓and ␈↓¬SAME-DEF. ␈↓
␈↓ ↓H␈↓␈↓ ¬wChapter  IV␈↓ *91



␈↓ ↓H␈↓␈↓¬SFF-ATOMXY: ␈↓␈↓ β(␈↓↓∀x y: [ATOM x ∨ ATOM y] ⊃ samefringef[x,y] = x ␈↓αequal␈↓↓ y␈↓
␈↓ ↓H␈↓␈↓¬SFF-PAIRXY: ␈↓␈↓ β(␈↓↓∀xx yy: samefringef[xx,yy] = ␈↓
␈↓ ↓H␈↓␈↓ ∧λ␈↓↓␈↓αa|␈↓↓gopher xx ␈↓αequal␈↓↓ ␈↓αa|␈↓↓gopher yy ␈↓αand␈↓↓ samefringef[␈↓αd|␈↓↓gopher xx, ␈↓αd|␈↓↓gopher y]␈↓

␈↓ ↓H␈↓Since␈α$recursive␈α$calls␈α$occur␈α$only␈α$in␈α$the␈α$␈↓↓PAIRXY␈↓␈α$case␈α$and␈α$are␈α$of␈α$the␈α#form
␈↓ ↓H␈↓␈↓↓samefringef[␈↓αd|␈↓↓gopher xx,␈↓αd|␈↓↓gopher yy]␈↓␈αthis␈αsuggests␈αthat␈αto␈αprove␈αproperties␈αof␈α␈↓↓samefringef␈↓␈α(and␈αof
␈↓ ↓H␈↓␈↓↓samefringe␈↓) we need to find a rank function ␈↓↓␈↓πr␈↓↓[x,y]␈↓ such that ␈↓ ∧↑␈↓↓∀xx yy: ␈↓πr␈↓↓[␈↓αd|␈↓↓gopher xx,␈↓αd|␈↓↓gopher yy] < 
␈↓ ↓H␈↓ ↓e␈↓↓␈↓πr␈↓↓[xx,yy].␈↓ Proof can then be done using induction on the rank as explained in Chapter III ␈↓π∞␈↓8. 

␈↓ ↓H␈↓Recall that we proved in Chapter III ␈↓π∞␈↓8

␈↓ ↓H␈↓␈↓¬PAIR-GOPHER: ␈↓␈↓ β(␈↓↓∀xx: PAIR gopher xx␈↓
␈↓ ↓H␈↓␈↓¬SIZE-GOHPER: ␈↓␈↓ β(␈↓↓∀xx: size gopher xx = size xx␈↓

␈↓ ↓H␈↓Thus␈α⊃either␈α⊃␈↓↓␈↓πr␈↓↓[x,y]=size␈α⊃x+size␈α⊂y␈↓␈α⊃or␈α⊃simply␈α⊃␈↓↓rho[x,y]␈α⊂=␈α⊃size␈α⊃x␈↓␈α⊃will␈α⊂do␈α⊃as␈α⊃a␈α⊃rank␈α⊃function␈α⊂for
␈↓ ↓H␈↓␈↓↓samefringe.␈↓  Since for either version of ␈↓πr␈↓ we have

␈↓ ↓H␈↓␈↓ ∧"␈↓↓∀xx yy: ␈↓πr␈↓↓[␈↓αd|␈↓↓gopher xx,␈↓αd|␈↓↓gopher yy] < ␈↓πr␈↓↓[xx,yy]␈↓ 

␈↓ ↓H␈↓we see that to prove ␈↓↓x y: ␈↓πF␈↓↓[x,y]␈↓ by on rank it is sufficient to prove

␈↓ ↓H␈↓␈↓¬PHI-ATOMXY: ␈↓␈↓ β(␈↓↓∀ x y: [ATOM x ∨ ATOM y]⊃␈↓πF␈↓↓[x,y]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓¬PHI-PAIRXY: ␈↓␈↓ β(␈↓↓∀xx yy: ␈↓πF␈↓↓[␈↓αd|␈↓↓gopher xx,␈↓αd|␈↓↓gopher yy] ⊃ ␈↓πF␈↓↓[xx,yy]␈↓

␈↓ ↓H␈↓        To␈α
prove␈α
that␈α
correctness␈αof␈α
␈↓↓samefringe␈↓␈α
we␈α
first␈α
prove␈αthat␈α
the␈α
program␈α
terminates␈α
on␈αall
␈↓ ↓H␈↓S-expression␈α∞pairs,␈α∞thus␈α∞giving␈α∂up␈α∞a␈α∞complete␈α∞description␈α∞of␈α∂the␈α∞predicate.␈α∞ Thus␈α∞we␈α∂prove␈α∞by
␈↓ ↓H␈↓induction on the rank ␈↓πr␈↓ that

␈↓ ↓H␈↓␈↓¬S-SAMEFR: ␈↓␈↓ β(␈↓↓∀x y: SEXP samefringef[x,y].␈↓

␈↓ ↓H␈↓which according to the above discussion reduces to proving

␈↓ ↓H␈↓␈↓¬S-ATOMXY: ␈↓␈↓ β(␈↓↓∀ x y: [ATOM x ∨ ATOM y]⊃SEXP samefringef[x,y]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓¬S-PAIRXY: ␈↓␈↓ β(␈↓↓∀xx yy: SEXP samefringef[␈↓αd|␈↓↓gopher xx,␈↓αd|␈↓↓gopher yy] ⊃  SEXP samefringef[xx,yy]␈↓

␈↓ ↓H␈↓␈↓¬S-ATOMXY␈α∩␈↓follows␈α∩directly␈α∪from␈α∩␈↓¬SFF-ATOMXY␈α∩␈↓and␈α∩the␈α∪domain␈α∩map␈α∩for␈α∩␈↓αequal␈↓␈α∪and␈α∩␈↓¬S-PAIRXY
␈↓ ↓H␈↓¬␈↓follows directly from ␈↓¬SFF-PAIRXY,PAIR-GOPHER, ␈↓ and the domain maps for ␈↓αequal␈↓ and ␈↓αand␈↓.

␈↓ ↓H␈↓We now obtain a characterization of ␈↓↓samefringe␈↓ in the two cases ␈↓↓ATOMXY␈↓ and ␈↓↓PAIRXY.␈↓

␈↓ ↓H␈↓␈↓¬SF-ATOMXY: ␈↓␈↓ β_␈↓↓∀x y: [[ATOM x ∨ ATOM y] ⊃ samefringe[x,y] ≡ x = y]␈↓
␈↓ ↓H␈↓␈↓¬SF-PAIRXY: ␈↓␈↓ β_␈↓↓∀xx yy: [samefringe[xx,yy] ≡ ␈↓
␈↓ ↓H␈↓␈↓ ∧_␈↓↓␈↓αa|␈↓↓gopher xx = ␈↓αa|␈↓↓gopher yy ∧ samefringe[␈↓αd|␈↓↓gopher xx, ␈↓αd|␈↓↓gopher y] ]␈↓

␈↓ ↓H␈↓These␈α∪facts␈α∪follow␈α∪directly␈α∪from␈α∩␈↓¬SAMEFRINGE-DEF,SFF-ATOMXY,SFF-PAIRXY,S-SAMEFR,PAIR-
␈↓ ↓H␈↓¬GOPHER, ␈↓ and the ␈↓¬EQ- ␈↓theorems given in Chapter III ␈↓π∞␈↓7.
␈↓ ↓H␈↓92␈↓ ¬wChapter  IV␈↓ H


␈↓ ↓H␈↓Now␈α∂we␈α∂are␈α∂ready␈α∂to␈α∂prove␈α∂␈↓¬SAMEFRINGE-COR.␈α∂␈↓␈α∂Using␈α∂the␈α∂same␈α∂induction␈α∂principle␈α∂as␈α⊂for␈α∂␈↓¬S-
␈↓ ↓H␈↓¬SAMEFR ␈↓we see that we need only prove

␈↓ ↓H␈↓␈↓¬COR-ATOMXY: ␈↓␈↓ β(␈↓↓∀ x y: [[ATOM x ∨ ATOM y]⊃[samefringe[x,y]≡fringe x=fringe y]]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓¬COR-PAIRXY: ␈↓␈↓ β(␈↓↓∀xx yy: [[samefringe[␈↓αd|␈↓↓gopher xx,␈↓αd|␈↓↓gopher yy]≡ ␈↓
␈↓ ↓H␈↓␈↓ ∧(␈↓↓fringe ␈↓αd|␈↓↓gopher xx=fringe ␈↓αd|␈↓↓gopher yy] ⊃ ␈↓
␈↓ ↓H␈↓␈↓ ∧λ␈↓↓[samefringe[xx,yy]≡fringe xx=fringe yy] ]␈↓

␈↓ ↓H␈↓In order to complete the proof we need some lemmas about ␈↓↓fringe␈↓ and ␈↓↓gopher:␈↓

␈↓ ↓H␈↓␈↓¬FRINGE-ATOMXY: ␈↓␈↓ βx␈↓↓∀ x y: [[ATOM x ∨ ATOM y]⊃[fringe x=fringe y≡x=y]]␈↓
␈↓ ↓H␈↓␈↓¬CAR-GOPHER-FRINGE: ␈↓␈↓ βx␈↓↓∀xx: ␈↓αa|␈↓↓fringe xx = ␈↓αa|␈↓↓gopher xx␈↓
␈↓ ↓H␈↓␈↓¬CDR-GOHPER-FRINGE: ␈↓␈↓ βx␈↓↓∀xx: ␈↓αd|␈↓↓fringe xx = fringe ␈↓αd|␈↓↓gopher xx␈↓

␈↓ ↓H␈↓␈↓¬CAR-GOPHER-FRINGE␈α⊗␈↓and␈α⊗␈↓¬CDR-GOHPER-FRINGE␈α↔␈↓were␈α⊗given␈α⊗as␈α↔exercises␈α⊗in␈α⊗Chapter␈α↔III␈α⊗␈↓π∞␈↓8.
␈↓ ↓H␈↓␈↓¬FRINGE-ATOMXY␈α␈↓follows␈αeasily␈αfrom␈α␈↓↓∀x a: [fringe x=fringe a≡x=a]␈↓␈αWhich␈αcan␈αbe␈αproved␈αas␈α
follows.
␈↓ ↓H␈↓If␈α∞␈↓↓x␈↓␈α
is␈α∞an␈α∞atom␈α
Then␈α∞by␈α∞␈↓¬FRINGE-ATOM␈α
␈↓we␈α∞need␈α
only␈α∞show␈α∞␈↓↓<x>=<a>≡x=a␈↓␈α
which␈α∞is␈α∞follows␈α
from
␈↓ ↓H␈↓␈↓¬EQPAIR␈α
␈↓(proved␈α
in␈αChapter␈α
III␈α
␈↓π∞␈↓2).␈α If␈α
␈↓↓x␈↓␈α
is␈α
not␈αan␈α
atom␈α
then␈α␈↓↓ATOM∩PAIR=␈↓πf␈↓↓␈↓␈α
tells␈α
us␈α
that␈α␈↓↓x≠a␈↓
␈↓ ↓H␈↓and␈α∂we␈α∂need␈α∂only␈α∞show␈α∂␈↓↓fringe␈α∂x≠<a>␈↓.␈α∂ One␈α∞way␈α∂to␈α∂see␈α∂this␈α∞is␈α∂to␈α∂observe␈α∂that␈α∂␈↓↓␈↓αd|␈↓↓<a>=␈↓¬NIL␈↓↓␈↓␈α∞and
␈↓ ↓H␈↓␈↓↓NELIST ␈↓αd|␈↓↓fringe x␈↓.  We leave the details to the reader.

␈↓ ↓H␈↓        Now␈α∂to␈α∂complete␈α∞the␈α∂proof␈α∂of␈α∞␈↓¬COR-SAMEFRINGE.␈α∂␈↓␈α∂␈↓¬COR-ATOMXY␈α∞␈↓follows␈α∂directly␈α∂from␈α∞␈↓¬SF-
␈↓ ↓H␈↓¬ATOMXY ␈↓and ␈↓¬FRINGE-ATOMXY. ␈↓ To prove ␈↓¬COR-PAIRXY ␈↓we assume

␈↓ ↓H␈↓␈↓ α←␈↓↓samefringe[␈↓αd|␈↓↓gopher xx,␈↓αd|␈↓↓gopher yy]≡fringe ␈↓αd|␈↓↓gopher xx=fringe ␈↓αd|␈↓↓gopher yy] ␈↓ 

␈↓ ↓H␈↓then by ␈↓¬SF-PAIRXY ␈↓it is sufficient to prove

␈↓ ↓H␈↓␈↓ α!␈↓↓[␈↓αa|␈↓↓gopher xx=␈↓αa|␈↓↓gopher yy ∧ fringe ␈↓αd|␈↓↓gopher xx=fringe ␈↓αd|␈↓↓gopher yy]≡fringe xx=fringe yy␈↓ 

␈↓ ↓H␈↓Using ␈↓¬CAR-GOPHER-FRINGE ␈↓and ␈↓¬CDR-GOPHER-FRINGE ␈↓this reduces to:

␈↓ ↓H␈↓␈↓ β	␈↓↓[␈↓αa|␈↓↓fringe xx=␈↓αa|␈↓↓fringe yy ∧ ␈↓αd|␈↓↓fringe xx=␈↓αd|␈↓↓fringe yy]≡fringe xx=fringe yy␈↓ 

␈↓ ↓H␈↓which is just ␈↓¬EQPAIR. ␈↓

␈↓ ↓H␈↓        Another version of ␈↓↓samefringe␈↓ without any auxiliary functions is

␈↓ ↓H␈↓␈↓¬SAMEFR1: ␈↓␈↓ αX␈↓↓samefringe[x, y] ← ␈↓
␈↓ ↓H␈↓␈↓ β_␈↓↓x ␈↓αequal␈↓↓ y ␈↓αor␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ β_␈↓↓[␈↓αnot␈↓↓ ␈↓αat|␈↓↓x ␈↓αand␈↓↓ ␈↓αnot␈↓↓ ␈↓αat|␈↓↓y ␈↓αand␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓[␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓x ␈↓αthen␈↓↓ [␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αa|␈↓↓y ␈↓αthen␈↓↓ ␈↓αa|␈↓↓x ␈↓αequal␈↓↓ ␈↓αa|␈↓↓y ␈↓αand␈↓↓ samefringe[␈↓αd|␈↓↓x,␈↓αd|␈↓↓y] ␈↓
␈↓ ↓H␈↓␈↓ ∧X␈↓↓␈↓αelse␈↓↓ samefringe[x,␈↓αaa|␈↓↓y. [␈↓αda|␈↓↓y . ␈↓αd|␈↓↓y]]]␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓ ␈↓αelse␈↓↓ samefringe[␈↓αaa|␈↓↓x . [␈↓αda|␈↓↓x . ␈↓αd|␈↓↓x], y]. ␈↓

␈↓ ↓H␈↓Note that a recursive call to ␈↓↓samefringe␈↓ does one of the following three things:

␈↓ ↓H␈↓        1)  decreases  ␈↓↓size x + size y␈↓
␈↓ ↓H␈↓        2)  leaves  ␈↓↓size x + size y␈↓ and ␈↓↓size ␈↓αa|␈↓↓x␈↓ invariant and decreases ␈↓↓size ␈↓αa|␈↓↓y␈↓
␈↓ ↓H␈↓        3)  leaves  ␈↓↓sixe x + size y␈↓ and ␈↓↓size ␈↓αa|␈↓↓y␈↓ invariant and decreases ␈↓↓size ␈↓αa|␈↓↓x ␈↓.
␈↓ ↓H␈↓␈↓ ¬wChapter  IV␈↓ *93


␈↓ ↓H␈↓This␈α∪can␈α∩lead␈α∪to␈α∪a␈α∩choice␈α∪of␈α∩an␈α∪induction␈α∪axiom␈α∩schema␈α∪in␈α∩at␈α∪least␈α∪two␈α∩ways.␈α∪ If␈α∪in␈α∩the
␈↓ ↓H␈↓␈↓¬NUMINDUCTION-CVI␈α⊃␈↓schema␈α⊂we␈α⊃let␈α⊂␈↓↓n␈↓␈α⊃and␈α⊂␈↓↓m␈↓␈α⊃range␈α⊂over␈α⊃all␈α⊂ordinals␈α⊃less␈α⊂than␈α⊃a␈α⊂given␈α⊃one␈α⊂it
␈↓ ↓H␈↓becomes␈αa␈αschema␈αof␈α
transfinite␈αinduction.␈αOrdinary␈αinduction␈αis␈α
obtained␈αas␈αa␈αspecial␈αcase␈α
where
␈↓ ↓H␈↓the␈αbounding␈αordinal␈αis␈α
␈↓πw␈↓␈αthe␈αleast␈αtransfinite␈α
ordinal.␈α If␈αwe␈αtake␈α
the␈αbounding␈αordinal␈αto␈αbe␈α
␈↓πw␈↓␈↓#
␈↓π␈↓#
w␈↓␈↓#
␈↓#
␈↓ ↓H␈↓then␈α
the␈α
SAMEFRINGE␈α
theorem␈α
for␈α
the␈α
above␈α
version␈α
of␈α
␈↓↓samefringe␈↓␈α
can␈α
be␈α
proved␈α
using␈α
the
␈↓ ↓H␈↓predicate

␈↓ ↓H␈↓␈↓ β≤␈↓↓␈↓πF␈↓↓ n  ≡ ∀x y: [[size x +size y]␈↓πw␈↓↓ + size ␈↓αa|␈↓↓x + size ␈↓αa|␈↓↓y = n ⊃ ␈↓¬THM␈↓↓[x, y]]␈↓ 

␈↓ ↓H␈↓        Alternately one could axiomatize the lexicographic ordering of triples of numbers by

␈↓ ↓H␈↓␈↓ β0␈↓↓∀l␈↓β1␈↓↓ m␈↓β1␈↓↓ n␈↓β1␈↓↓ l m n: [(l␈↓β1␈↓↓, m␈↓β1␈↓↓, n␈↓β1␈↓↓) < (l, m, n) ≡                         ␈↓
␈↓ ↓H␈↓␈↓ β]␈↓↓[l␈↓β1␈↓↓ < l] ∨ [l␈↓β1␈↓↓ = l ∧ m␈↓β1␈↓↓ < m] ∨ [l␈↓β1␈↓↓ = l ∧ m␈↓β1␈↓↓ = m ∧ n␈↓β1␈↓↓ < n]]␈↓

␈↓ ↓H␈↓This␈α∞ordering␈α∞is␈α
well-founded␈α∞(has␈α∞no␈α∞infinite␈α
decreasing␈α∞sequences)␈α∞and␈α
so␈α∞we␈α∞have␈α∞a␈α
schema
␈↓ ↓H␈↓analogous to ␈↓¬NUMINDUCTION-CVI ␈↓given by

␈↓ ↓H␈↓␈↓ α]␈↓↓∀l m n: [∀l␈↓β1␈↓↓ m␈↓β1␈↓↓ n␈↓β1␈↓↓: [(l␈↓β1␈↓↓, m␈↓β1␈↓↓, n␈↓β1␈↓↓) < (l, m, n) ⊃␈↓πF␈↓↓(l␈↓β1␈↓↓, m␈↓β1␈↓↓, n␈↓β1␈↓↓)] ⊃ ␈↓πF␈↓↓(l, m, n)]␈↓
␈↓ ↓H␈↓␈↓ ¬A␈↓↓⊃ ∀l m n: ␈↓πF␈↓↓(l, m, n)␈↓

␈↓ ↓H␈↓The SAMEFRINGE theorems can now be proved using this schema with the predicate

␈↓ ↓H␈↓␈↓ αS␈↓↓␈↓πF␈↓↓(l, m, n) ≡ ∀x y: [l = size x +size y ∧ m = size ␈↓αa|␈↓↓y ∧ n = size ␈↓αa|␈↓↓x ⊃ ␈↓¬THM␈↓↓[x, y]]␈↓ 

␈↓ ↓H␈↓        The␈α⊃minimization␈α⊃schema␈α∩provides␈α⊃an␈α⊃alternate␈α⊃method␈α∩for␈α⊃proving␈α⊃the␈α∩correctness␈α⊃of
␈↓ ↓H␈↓␈↓↓samefringe.␈↓␈α To␈αsimplify␈αmatters␈αwe␈αeliminate␈αthe␈αauxiliary␈αfunction␈α␈↓↓same␈↓␈αfrom␈αthe␈αdefinition␈αof
␈↓ ↓H␈↓␈↓↓samefringe␈↓␈α⊂thus␈α⊂eliminating␈α⊂the␈α∂problem␈α⊂of␈α⊂having␈α⊂to␈α∂deal␈α⊂with␈α⊂mutually␈α⊂recursive␈α∂programs.
␈↓ ↓H␈↓The functional defining ␈↓↓samefringe␈↓ now becomes

␈↓ ↓H␈↓␈↓ αX␈↓↓␈↓πt␈↓↓␈↓βsf␈↓↓ = λf: λx y: [[x ␈↓αequal␈↓↓ y] ␈↓αor␈↓↓ [[␈↓αnot␈↓↓ ␈↓αat|␈↓↓x ␈↓αand␈↓↓ ␈↓αnot␈↓↓ ␈↓αat|␈↓↓y] ␈↓αand␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βX␈↓↓[[␈↓αa|␈↓↓gopher x ␈↓αequal␈↓↓ ␈↓αa|␈↓↓gopher y] ␈↓αand␈↓↓ f[␈↓αd|␈↓↓gopher x,␈↓αd|␈↓↓gopher y]]]] ␈↓.

␈↓ ↓H␈↓Now␈αwe␈αform␈αan␈αaxiom␈αschema␈αfrom␈αthe␈αminimization␈αschema␈αby␈αusing␈αthe␈α␈↓πt␈↓␈↓βsf␈↓␈αgiven␈αabove␈αand
␈↓ ↓H␈↓␈↓↓samefringe␈↓ for ␈↓↓f.␈↓↓j
␈↓ ↓H␈↓␈↓ α,␈↓↓∀x y: [SEXP ␈↓πt␈↓↓␈↓βsf␈↓↓[F,x,y] ⊃ F[x,y] = ␈↓πt␈↓↓␈↓βsf␈↓↓[F,x,y]] ⊃  ∀x y: [SEXP samefringe[x,y] ⊃ 
␈↓ ↓H␈↓ ¬↔␈↓↓samefringe[x,y] = F[x,y]] ␈↓. 

␈↓ ↓H␈↓We instantiate this schema with

␈↓ ↓H␈↓␈↓ ∧⊃␈↓↓F[x, y] = fringe x ␈↓αequal␈↓↓ fringe y  .                ␈↓ 

␈↓ ↓H␈↓The proof then consists of the following steps
␈↓ ↓H␈↓94␈↓ ¬wChapter  IV␈↓ H



␈↓ ↓H␈↓Prove
␈↓ ↓H␈↓        1)      ␈↓↓∀x y: [F[x, y] = ␈↓πt␈↓↓␈↓βsf␈↓↓[F] [x, y]]␈↓
␈↓ ↓H␈↓        2)      ␈↓↓∀x y:[SEXP samefringef[x,y]␈↓
␈↓ ↓H␈↓conclude from 1) - 2) and the minimization schema instantiatio
␈↓ ↓H␈↓        3)      ␈↓↓∀x y: [fringe x ␈↓αequal␈↓↓ fringe y = samefringef[x, y]]␈↓
␈↓ ↓H␈↓and from the definition of ␈↓↓samefringe␈↓ 2) and 3) conclude
␈↓ ↓H␈↓        5)      ␈↓↓∀x y: [samefringe[x, y] ≡ fringe x = fringe y]␈↓
␈↓ ↓H␈↓as desired.



␈↓ ↓H␈↓2.  ␈↓αCorrectness of a program to partition lists.␈↓


␈↓ ↓H␈↓        Consider␈α∂the␈α∂problem␈α⊂of␈α∂determining␈α∂all␈α⊂partitions␈α∂of␈α∂a␈α∂list␈α⊂into␈α∂some␈α∂number␈α⊂of␈α∂(non-
␈↓ ↓H␈↓empty) sublists.  We say that a list ␈↓↓w␈↓ is a partition of another list ␈↓↓u␈↓ into ␈↓↓n␈↓ parts if:

␈↓ ↓H␈↓␈↓ α_1. each element of ␈↓↓w␈↓ is a nonempty list,
␈↓ ↓H␈↓␈↓ α_2. the length of ␈↓↓w␈↓ is ␈↓↓n,␈↓ and
␈↓ ↓H␈↓␈↓ α_3. the result of ␈↓↓append␈↓ing the elements of ␈↓↓w␈↓ together (in order) is ␈↓↓u.␈↓

␈↓ ↓H␈↓Thus ␈↓¬((A B) (C) (D))␈↓ is a partition of the list ␈↓¬(A B C D)␈↓ into 3 parts, and

␈↓ ↓H␈↓␈↓ β@␈↓↓␈↓¬(((A B) (C) (D)) ((A) (B C) (D)) ((A) (B) (C D)))␈↓↓␈↓ 

␈↓ ↓H␈↓is␈αthe␈αlist␈αof␈αall␈αpartitions␈αof␈α␈↓¬(A␈αB␈αC␈αD)␈↓␈αinto␈α3␈αparts.␈α Note␈αthat␈αfor␈αlists␈αthe␈αorder␈αof␈αthe␈αelements
␈↓ ↓H␈↓of␈αthe␈αpartition␈αis␈αimportant,␈αin␈αcontrast␈αthe␈α
usual␈αnotion␈αof␈αpartitions␈αof␈αa␈αnatural␈αnumber␈αinto␈α
a
␈↓ ↓H␈↓list of summands.

␈↓ ↓H␈↓       In␈α∩order␈α⊃to␈α∩make␈α⊃the␈α∩discussion␈α⊃of␈α∩partitions␈α⊃easier,␈α∩we␈α⊃will␈α∩introduce␈α⊃some␈α∩new␈α⊃sorts:
␈↓ ↓H␈↓␈↓↓UULIST␈↓␈α
(lists␈α
whose␈α
members␈α∞are␈α
non-empty␈α
lists)␈α
made␈α∞up␈α
of␈α
the␈α
subsorts␈α∞␈↓↓NEUULIST␈↓␈α
(non-
␈↓ ↓H␈↓empty␈αlists␈αof␈αnon-empty␈αlists)␈αand␈α␈↓↓NULL,␈↓␈α ␈↓↓PNLIST␈↓␈α(partition␈αlists,␈αe.g.␈αthe␈αmembers␈αare␈αof␈αsort
␈↓ ↓H␈↓␈↓↓UULIST␈↓)␈α∪made␈α∪up␈α∪of␈α∪the␈α∪subsorts␈α∪␈↓↓NEPNLIST␈↓␈α∪and␈α∪␈↓↓NULL,␈↓␈α∪and␈α∪␈↓↓PNNLIST␈↓␈α∪(non␈α∪trivial
␈↓ ↓H␈↓partition␈αlists,␈αe.g.␈αthe␈αmembers␈αare␈αof␈αsort␈α␈↓↓NEUULIST␈↓)␈αmade␈αup␈αof␈αthe␈αsubsorts␈α␈↓↓NEPNNLIST␈↓
␈↓ ↓H␈↓and ␈↓↓NULL.␈↓  We will have additional variables ranging over the new sorts.

␈↓ ↓H␈↓␈↓ βx␈↓↓uul, vvl, wwl ε UULIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓uull, vvll, wwll ε NEUULIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓pl, ql, rl ε PNLIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓pll, qll, rll ε NEPNLIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓pnnl, qnnl, rnnl ε PNNLIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓pnnll, qnnll, rnnll ε NEPNNLIST␈↓

␈↓ ↓H␈↓And␈α
we␈α
will␈αhave␈α
definition␈α
of␈α
programs␈αby␈α
recursion␈α
on␈α
the␈αnew␈α
domains␈α
(analogous␈αto␈α
ordinary
␈↓ ↓H␈↓list␈α
recursion)␈α
and␈αcorresponding␈α
structural␈α
induction␈α
principles.␈α  A␈α
formal␈α
description␈α
of␈αthe␈α
new
␈↓ ↓H␈↓sorts is given at the end of this section.

␈↓ ↓H␈↓        In␈α∞order␈α∞to␈α∞formalize␈α∞the␈α∂notion␈α∞of␈α∞a␈α∞partition,␈α∞we␈α∞need␈α∂to␈α∞specify␈α∞what␈α∞is␈α∞meant␈α∂by␈α∞the
␈↓ ↓H␈↓phrase␈α∩"appending␈α∩elements␈α∪of␈α∩a␈α∩list␈α∩together".␈α∪ The␈α∩program␈α∩␈↓↓combine␈↓␈α∩appends␈α∪together␈α∩the
␈↓ ↓H␈↓elements of a partition and is defined using ␈↓↓UULIST␈↓ recursion by:
␈↓ ↓H␈↓␈↓ ¬wChapter  IV␈↓ *95


␈↓ ↓H␈↓␈↓ βS␈↓↓combine uul ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓uul ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓uul * combine ␈↓αd|␈↓↓uul␈↓ 

␈↓ ↓H␈↓and represented by the function ␈↓↓combine␈↓ which satisfies the axiom:

␈↓ ↓H␈↓␈↓¬COMBINE: ␈↓␈↓ β.␈↓↓∀uul: combine uul = ␈↓αif␈↓↓ ␈↓αn|␈↓↓uul ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αa|␈↓↓uul * combine ␈↓αd|␈↓↓uul␈↓

␈↓ ↓H␈↓from which we easily deduce

␈↓ ↓H␈↓␈↓ αX␈↓¬COMBINE-NIL: ␈↓␈↓↓combine ␈↓¬NIL␈↓↓ = ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ αX␈↓¬COMBINE-ULL: ␈↓␈↓↓∀uull: combine uull =␈↓αa|␈↓↓uull * combine ␈↓αd|␈↓↓uull␈↓

␈↓ ↓H␈↓If␈αwe␈α
restrict␈αthe␈α
domain␈αof␈α
␈↓↓append␈↓␈αto␈α
␈↓↓UULIST␈↓␈α␈↓↓PNLIST␈↓␈α
or␈α␈↓↓PNNLIST␈↓␈α
we␈αobtain␈αthe␈α
additional
␈↓ ↓H␈↓domain mappings for ␈↓↓append:␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓*: UULIST ␈↓¬x␈↓↓ UULIST → UULIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓*: PNLIST ␈↓¬x␈↓↓ PNLIST → PNLIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓*: PNNLIST ␈↓¬x␈↓↓ PNNLIST → PNNLIST␈↓

␈↓ ↓H␈↓Using the facts specfied by the function mappings for ␈↓↓append␈↓ we have

␈↓ ↓H␈↓␈↓ βx␈↓↓combine: UULIST → LIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓combine: NEUULIST → NELIST␈↓

␈↓ ↓H␈↓Now␈αwe␈αcan␈αgive␈αa␈αformal␈αdefinition␈αof␈αwhat␈αwe␈αmean␈αby␈αpartition.␈α We␈αintroduce␈αthe␈αpredicate
␈↓ ↓H␈↓␈↓↓Ispartn␈↓ with the defining axiom

␈↓ ↓H␈↓␈↓¬ISPARTN-DEF: ␈↓
␈↓ ↓H␈↓␈↓ α1␈↓↓∀U N W:[Ispartn[W,U,N] ≡ ∃uul.(W =uul ∧ length uul = N ∧ combine uul = U]]␈↓

␈↓ ↓H␈↓which␈αis␈αa␈αdirect␈αformalization␈αof␈αthe␈αthree␈αproperties␈αof␈αa␈αpartition␈αgiven␈αabove.␈α Given␈αa␈αlist␈α␈↓↓u␈↓
␈↓ ↓H␈↓and a number ␈↓↓n␈↓ we want a list ␈↓↓pl␈↓ of all partitions of ␈↓↓u␈↓ into ␈↓↓n␈↓ parts.

␈↓ ↓H␈↓        Before␈α
we␈α∞give␈α
the␈α
program␈α∞computing␈α
␈↓↓pl,␈↓␈α
we␈α∞analyze␈α
some␈α
properties␈α∞of␈α
partitions␈α∞of␈α
a
␈↓ ↓H␈↓list.␈α⊃ First,␈α⊃the␈α⊃degenerate␈α⊃cases␈α⊃where␈α⊃␈↓↓u␈↓␈α⊃is␈α⊃␈↓¬NIL␈↓␈α⊃or␈α⊃␈↓↓n␈↓␈α⊂is␈α⊃␈↓¬0.␈α⊃␈↓␈α⊃If␈α⊃␈↓↓u␈α⊃=␈α⊃␈↓¬NIL␈↓↓␈↓␈α⊃then␈α⊃it␈α⊃can␈α⊃not␈α⊂be
␈↓ ↓H␈↓partitioned␈α∪into␈α∪non-empty␈α∪lists,␈α∪so␈α∪the␈α∪only␈α∪possibility␈α∪is␈α∪the␈α∪partition␈α∪which␈α∪itself␈α∪is␈α∪␈↓¬NIL␈↓
␈↓ ↓H␈↓partitioning␈α⊂of␈α⊃␈↓↓u␈↓␈α⊂into␈α⊂␈↓¬0␈α⊃␈↓parts.␈α⊂ If␈α⊂␈↓↓u␈↓␈α⊃is␈α⊂non-empty␈α⊃then␈α⊂there␈α⊂must␈α⊃be␈α⊂at␈α⊂least␈α⊃␈↓¬1␈α⊂␈↓part␈α⊃in␈α⊂the
␈↓ ↓H␈↓partition,␈αe.g.␈α␈↓↓n␈α≠␈α0␈↓.␈α This␈αis␈αexpressed␈αby␈αthe␈αfollowing␈αlemmas.␈α Also␈αnote␈αthat␈αin␈αthe␈αdefinition
␈↓ ↓H␈↓of ␈↓↓Ispartn,␈↓ if ␈↓↓n ≠ 0␈↓ we may choose the ␈↓↓UULIST␈↓ to be non-empty.

␈↓ ↓H␈↓␈↓ αλ␈↓¬ISPARTN-NIL-ZERO: ␈↓␈↓↓∀W:[Ispartn[W,␈↓¬NIL␈↓↓,0] ≡ W = ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓¬ISPARTN-NIL-NN: ␈↓␈↓↓∀nn W:¬Ispartn[W,␈↓¬NIL␈↓↓,nn]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓¬ISPARTN-UU-ZERO: ␈↓␈↓↓∀uu W:¬Ispartn[W,uu,0]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓¬ISPARTN-DEF-NN: ␈↓
␈↓ ↓H␈↓␈↓ βλ␈↓↓∀uu nn W:[Ispartn[W,uu,nn]≡∃uull:[W=uull∧length uull=nn∧combine uull=uu]]␈↓


␈↓ ↓H␈↓These␈α∞lemmas␈α∞are␈α∞easily␈α
proved␈α∞using␈α∞the␈α∞definitions␈α∞of␈α
␈↓↓Ispartn,␈↓␈α∞␈↓↓length␈↓␈α∞and␈α∞␈↓↓combine,␈↓␈α∞the␈α
fact
␈↓ ↓H␈↓that␈αthe␈αonly␈αthe␈αempty␈αlist␈αhas␈αlength␈α0␈αand␈αonly␈αthe␈αempty␈αlist␈αof␈αnon-empty␈αlists␈αcombines␈αinto
␈↓ ↓H␈↓␈↓¬NIL␈↓.
␈↓ ↓H␈↓96␈↓ ¬wChapter  IV␈↓ H


␈↓ ↓H␈↓        Now␈αwe␈αconsider␈αnon-trivial␈αpartitions␈αof␈αnon-empty␈αlists.␈α Consider␈αa␈αpartition␈α␈↓↓uull␈↓␈αof␈αthe
␈↓ ↓H␈↓non-empty␈αlist␈α␈↓↓uu␈↓␈αinto␈α␈↓↓nn␈↓␈αparts.␈αLet␈α␈↓↓ww=␈↓αa|␈↓↓uull␈↓.␈α Note␈αthat␈α␈↓↓ww␈↓␈αmust␈αbe␈αand␈αinitial␈αsegment␈αof␈α␈↓↓uu.␈↓
␈↓ ↓H␈↓There␈αare␈αtwo␈αposibilities.␈αEither␈α␈↓↓ww␈↓␈αis␈αthe␈αone␈αelement␈αlist␈α␈↓↓<␈↓αa|␈↓↓uu>␈↓,␈αor␈αit␈αis␈αa␈αlist␈αof␈αlength␈αgreater
␈↓ ↓H␈↓than␈αone␈α(e.g.␈α
␈↓↓␈↓αa|␈↓↓ww=␈↓αa|␈↓↓uu␈↓␈αand␈α␈↓↓␈↓αd|␈↓↓ww≠␈↓¬NIL␈↓↓␈↓.␈α
 In␈αthe␈αfirst␈α
case␈α␈↓↓␈↓αd|␈↓↓uull␈↓␈αis␈α
a␈αpartition␈αof␈α
␈↓↓␈↓αd|␈↓↓uu␈↓␈αinto␈α␈↓↓sub1␈α
nn␈↓
␈↓ ↓H␈↓parts.␈α
 In␈α
the␈α
second␈α
case␈α
the␈α
list␈α
formed␈α
by␈α
replacing␈α
␈↓↓ww␈↓␈α
by␈α
␈↓↓␈↓αd|␈↓↓ww␈↓␈α
(in␈α
␈↓↓W␈↓)␈α
is␈α
a␈α
partition␈α
of␈α␈↓↓␈↓αd|␈↓↓uu␈↓
␈↓ ↓H␈↓into ␈↓↓nn␈↓ parts.   Thus we have

␈↓ ↓H␈↓␈↓ ↓x␈↓¬ISPARTN-UU-NN: ␈↓
␈↓ ↓H␈↓␈↓ αx␈↓↓∀uu nn W:[Ispartn[W,uu,nn] ≡ ␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓∃uull:[W=uull∧␈↓αa|␈↓↓uull=<␈↓αa|␈↓↓uu>∧Ispartn[␈↓αd|␈↓↓uull,␈↓αd|␈↓↓uu,sub1 nn]] ∨␈↓
␈↓ ↓H␈↓␈↓ β8␈↓↓∃uull:[W=uull∧␈↓αaa|␈↓↓uull=␈↓αa|␈↓↓uu∧Ispartn[[␈↓αda|␈↓↓uull . ␈↓αd|␈↓↓uull],␈↓αd|␈↓↓uu,nn]]␈↓

␈↓ ↓H␈↓The␈αproof␈α
of␈α␈↓¬ISPARTN-UU-NN␈α␈↓uses␈α
just␈αthe␈α
definitions␈αof␈α␈↓↓Ispartn,␈↓␈α
␈↓↓length,␈↓␈αand␈α
␈↓↓combine␈↓␈αtogether
␈↓ ↓H␈↓with basic facts about numbers and S-expressions.  By ␈↓¬ISPARTN-DEF-NN ␈↓it is sufficient to show

␈↓ ↓H␈↓  ␈↓↓∃uull:[W=uull ∧ length uull=nn ∧ combine uull=uu]␈↓
␈↓ ↓H␈↓  ␈↓↓ ≡ ∃uull:[W=uull∧␈↓αa|␈↓↓uull=<␈↓αa|␈↓↓uu>∧Ispartn[␈↓αd|␈↓↓uull,␈↓αd|␈↓↓uu,sub1 nn]] ∨␈↓
␈↓ ↓H␈↓  ␈↓↓   ∃uull:[W=uull∧␈↓αaa|␈↓↓uull=␈↓αa|␈↓↓uu∧Ispartn[[␈↓αda|␈↓↓uull . ␈↓αd|␈↓↓uull],␈↓αd|␈↓↓uu,nn]]␈↓

␈↓ ↓H␈↓First assume

␈↓ ↓H␈↓␈↓¬ISUUNN1: ␈↓␈↓ ∧_␈↓↓W=uull ∧ length uull=nn and combine uull=uu␈↓. 

␈↓ ↓H␈↓Then␈αif␈αwe␈αsubsitute␈α␈↓↓combine␈αuull␈↓␈αfor␈α␈↓↓uu␈↓␈αand␈αuse␈α␈↓¬COMBINE-ULL,␈α␈↓␈↓¬APPEND-UU␈α␈↓␈αand␈αthe␈αbasic␈αfacts
␈↓ ↓H␈↓about S-expressions we obtain ␈↓↓␈↓αa|␈↓↓uull=␈↓αa|␈↓↓uu . ␈↓αda|␈↓↓uull␈↓ In the case: ␈↓↓␈↓αda|␈↓↓uull=␈↓¬NIL␈↓↓␈↓ we will show

␈↓ ↓H␈↓␈↓↓ ∃vvll:[W=vvll∧␈↓αa|␈↓↓vvll=<␈↓αa|␈↓↓uu>∧Ispartn[␈↓αd|␈↓↓vvll,␈↓αd|␈↓↓uu,sub1 nn]] ␈↓

␈↓ ↓H␈↓and in the case: ␈↓↓␈↓αda|␈↓↓uull≠␈↓¬NIL␈↓↓␈↓  we will show

␈↓ ↓H␈↓␈↓↓ ∃vvll:[W=vvll∧␈↓αaa|␈↓↓vvll=␈↓αa|␈↓↓uu∧Ispartn[[␈↓αda|␈↓↓vvll . ␈↓αd|␈↓↓vvll],␈↓αd|␈↓↓uu,nn]]␈↓

␈↓ ↓H␈↓thus␈αproving␈αthe␈α=>␈αdirection␈αof␈αthe␈αequivalence.␈α Assume␈α␈↓↓␈↓αda|␈↓↓uull=␈↓¬NIL␈↓↓␈↓␈αthen␈αif␈αwe␈αlet␈α␈↓↓uul=qd␈↓␈αuull␈↓↓␈↓
␈↓ ↓H␈↓we␈α∃have␈α∃by␈α⊗␈↓¬APPEND-NIL,␈α∃␈↓␈↓¬APPEND-UU,␈α∃␈↓␈↓¬LENGTH-UU,␈α⊗␈↓␈↓¬COMBINE-ULL␈α∃␈↓and␈α∃basic␈α⊗properties␈α∃of
␈↓ ↓H␈↓numbers and S-expressions

␈↓ ↓H␈↓␈↓ βx␈↓↓uul=␈↓αd|␈↓↓uull ∧ length uul=sub1 nn ∧ combine uul=␈↓αd|␈↓↓uu␈↓ 

␈↓ ↓H␈↓and by ␈↓¬ISPARTN-DEF ␈↓

␈↓ ↓H␈↓␈↓ βG␈↓↓W=uull ∧ ␈↓αd|␈↓↓uull=[␈↓αa|␈↓↓uu . ␈↓¬NIL␈↓↓]∧Ispartn[␈↓αd|␈↓↓uull,␈↓αd|␈↓↓uu,sub1 nn]␈↓ 

␈↓ ↓H␈↓completing␈α∪the␈α∪first␈α∩case.␈α∪ Now␈α∪assume␈α∩␈↓↓␈↓αda|␈↓↓uull≠␈↓¬NIL␈↓↓␈↓.␈α∪ Let␈α∪␈↓↓␈↓αda|␈↓↓uull=ww␈↓␈α∪and␈α∩␈↓↓[ww .␈↓αd|␈↓↓uull]=wwll␈↓.
␈↓ ↓H␈↓Then as above we have

␈↓ ↓H␈↓␈↓ αy␈↓↓ cons(cdr car uull,cdr uull)=wwll ∧ length wwll=nn ∧ combine wwll=␈↓αd|␈↓↓uu␈↓ 

␈↓ ↓H␈↓and by ␈↓¬ISPARTN-DEF ␈↓

␈↓ ↓H␈↓␈↓ βM␈↓↓W=uull ∧ ␈↓αaa|␈↓↓uull=␈↓αa|␈↓↓uu ∧ Ispartn[␈↓αda|␈↓↓uull . ␈↓αd|␈↓↓uull,␈↓αd|␈↓↓uu,nn]␈↓ 
␈↓ ↓H␈↓␈↓ ¬wChapter  IV␈↓ *97


␈↓ ↓H␈↓completing the second case and the proof of the => direction.

␈↓ ↓H␈↓To prove the <= direction there are also two cases:

␈↓ ↓H␈↓  ␈↓↓∃uull:[W=uull∧␈↓αa|␈↓↓uull=<␈↓αa|␈↓↓uu>∧Ispartn[␈↓αd|␈↓↓uull,␈↓αd|␈↓↓uu,sub1 nn]] ⊃␈↓
␈↓ ↓H␈↓  ␈↓↓∃uull:[W=uull ∧ length uull=nn ∧ combine uull=uu] ␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓  ␈↓↓∃uull:[W=uull∧␈↓αaa|␈↓↓uull=␈↓αa|␈↓↓uu∧Ispartn[[␈↓αda|␈↓↓uull . ␈↓αd|␈↓↓uull],␈↓αd|␈↓↓uu,nn]] ⊃␈↓
␈↓ ↓H␈↓  ␈↓↓∃uull:[W=uull ∧ length uull=nn ∧ combine uull=uu].␈↓

␈↓ ↓H␈↓If we assume

␈↓ ↓H␈↓␈↓ βj␈↓↓ W=uull∧␈↓αa|␈↓↓uull=<␈↓αa|␈↓↓uu>∧Ispartn[␈↓αd|␈↓↓uull,␈↓αd|␈↓↓uu,sub1 nn] ␈↓
␈↓ ↓H␈↓or
␈↓ ↓H␈↓␈↓ βH␈↓↓ W=uull∧␈↓αaa|␈↓↓uull=␈↓αa|␈↓↓uu∧Ispartn[[␈↓αda|␈↓↓uull . ␈↓αd|␈↓↓uull],␈↓αd|␈↓↓uu,nn]] ␈↓

␈↓ ↓H␈↓then by ␈↓¬ISPARTN-DEF, ␈↓␈↓¬APPEND-NIL, ␈↓␈↓¬APPEND-UU, ␈↓␈↓¬LENGTH-UU, ␈↓␈↓¬COMBINE-ULL ␈↓we have

␈↓ ↓H␈↓␈↓ ∧*␈↓↓W=uull ∧ length uull=nn ∧ combine uull=uu␈↓

␈↓ ↓H␈↓Which completes the proof of ␈↓¬ISPARTN-UU-NN. ␈↓

␈↓ ↓H␈↓        Notice␈α
that␈α␈↓¬ISPARTN-UU-NN␈α
␈↓implies␈αthat␈α
the␈αpartitions␈α
of␈α␈↓↓uu␈↓␈α
can␈αbe␈α
expressed␈αin␈α
terms␈αof
␈↓ ↓H␈↓the␈α
partitions␈α
of␈α
␈↓↓␈↓αd|␈↓↓uu␈↓.␈α
 Namely␈α
we␈α
take␈α
the␈α
partitions␈α
of␈α
␈↓↓␈↓αd|␈↓↓uu␈↓␈α
into␈α
␈↓↓sub1␈α
nn␈↓␈α
parts␈α
and␈α
add␈α
␈↓↓<␈↓αa|␈↓↓uu>␈↓␈α
as
␈↓ ↓H␈↓the␈α
first␈αelement␈α
of␈αeach␈α
partition.␈α
  To␈αthese␈α
we␈α␈↓↓append␈↓␈α
the␈α
result␈αof␈α
taking␈αthe␈α
partitions␈αof␈α
␈↓↓␈↓αd|␈↓↓uu␈↓
␈↓ ↓H␈↓into␈α
␈↓↓nn␈↓␈α
parts␈α
and␈α
adding␈α
␈↓↓␈↓αa|␈↓↓uu␈↓␈α
to␈α
the␈α
first␈α
element␈α
of␈α
each␈α
partition.␈α
 These␈α
two␈α
tacking␈α
procedures
␈↓ ↓H␈↓are␈αimplemented␈αby␈α
the␈αprograms␈α␈↓↓tack1␈↓␈α
and␈α␈↓↓tack0␈↓␈αdefined␈α
by␈α␈↓↓PNLIST␈↓␈αand␈α␈↓↓PNNLIST␈↓␈α
recursion
␈↓ ↓H␈↓respectively.

␈↓ ↓H␈↓␈↓¬TACK1: ␈↓␈↓ αX␈↓↓tack1[vv,pl] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓pl ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [vv . ␈↓αa|␈↓↓pl] . tack1[vv,␈↓αd|␈↓↓pl]␈↓
␈↓ ↓H␈↓␈↓¬TACK0: ␈↓␈↓ αX␈↓↓tack0[x,pnnl] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓pnnl ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [[x . ␈↓αaa|␈↓↓pnnl] . ␈↓αda|␈↓↓pnnl] . tack0[x,␈↓αd|␈↓↓pnnl]␈↓

␈↓ ↓H␈↓they are represented by the functions ␈↓↓tack0␈↓ and ␈↓↓tack1␈↓ satisfying

␈↓ ↓H␈↓␈↓¬TACK0-DEF: ␈↓␈↓ β_␈↓↓∀vv pl:tack1[vv,pl] = ␈↓αif␈↓↓ ␈↓αn|␈↓↓pl ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [vv . ␈↓αa|␈↓↓pl] . tack1[vv,␈↓αd|␈↓↓pl]␈↓
␈↓ ↓H␈↓␈↓ β_␈↓↓tack0: PNNLIST → PNNLIST␈↓

␈↓ ↓H␈↓␈↓¬TACK1-DEF: ␈↓␈↓ β_␈↓↓∀x pnnl:tack0[x,pnnl] = ␈↓αif␈↓↓ ␈↓αn|␈↓↓pnnl ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓↓ ␈↓αelse␈↓↓ [[x . ␈↓αaa|␈↓↓pnnl] . ␈↓αda|␈↓↓pnnl] . tack1[x,␈↓αd|␈↓↓pnnl]␈↓
␈↓ ↓H␈↓␈↓ β_␈↓↓tack1: PNLIST → PNNLIST␈↓

␈↓ ↓H␈↓and we have the immediate corollaries:

␈↓ ↓H␈↓␈↓¬TACK1-NIL: ␈↓␈↓ β_␈↓↓∀vv:tack1[vv,␈↓¬NIL␈↓↓] = ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓¬TACK1-PNNLL: ␈↓␈↓ β_␈↓↓∀vv pll: tack1[vv,pll] = [vv . ␈↓αa|␈↓↓pll] . tack1[vv,␈↓αd|␈↓↓pll]␈↓

␈↓ ↓H␈↓␈↓¬TACK0-NIL: ␈↓␈↓ β_␈↓↓∀x:tack0[x,␈↓¬NIL␈↓↓] = ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓¬TACK0-PNNLL: ␈↓␈↓ β_␈↓↓∀x pnnll: tack0[x,pnnll]=[[x . ␈↓αaa|␈↓↓pnnll] . ␈↓αda|␈↓↓pnnll] . tack1[x,␈↓αd|␈↓↓pnnll]␈↓

␈↓ ↓H␈↓        The following facts about ␈↓↓tack1␈↓ and ␈↓↓tack0␈↓ correspond to the clauses of ␈↓¬ISPARTN-UU-NN. ␈↓
␈↓ ↓H␈↓98␈↓ ¬wChapter  IV␈↓ H


␈↓ ↓H␈↓␈↓¬MEM-TACK1: ␈↓␈↓ β(␈↓↓∀vv pl W:[member[W,tack1[vv,pl]] ≡ ␈↓
␈↓ ↓H␈↓␈↓ ∧(␈↓↓∃uull:[W=uull∧␈↓αa|␈↓↓uull=vv∧member[␈↓αd|␈↓↓uull,pl]] ]␈↓
␈↓ ↓H␈↓␈↓¬MEM-TACK0: ␈↓␈↓ β(␈↓↓∀x pnnl W:[member[W,tack0[x,pnnl]] ≡ ␈↓
␈↓ ↓H␈↓␈↓ ∧(␈↓↓∃uull:[W=uull∧␈↓αaa|␈↓↓uull=x∧member[␈↓αda|␈↓↓uull . ␈↓αd|␈↓↓uull,pnnl]] ]␈↓

␈↓ ↓H␈↓The␈α∂proofs␈α∂of␈α∞␈↓¬MEM-TACK1␈α∂␈↓and␈α∂␈↓¬MEM-TACK0␈α∞␈↓are␈α∂similar.␈α∂ We␈α∞will␈α∂prove␈α∂only␈α∞␈↓¬MEM-TACK1.␈α∂␈↓␈α∂It␈α∞is
␈↓ ↓H␈↓proved by using the ␈↓¬PNLISTINDUCTION ␈↓schema.  In the case ␈↓↓pl=␈↓¬NIL␈↓↓␈↓ we must show

␈↓ ↓H␈↓␈↓ α.␈↓↓∀vv W:[member[W,tack1[vv,␈↓¬NIL␈↓↓]]≡∃uull:[W=uull∧␈↓αa|␈↓↓uull=vv∧member[␈↓αd|␈↓↓uull,␈↓¬NIL␈↓↓]] ].␈↓ 

␈↓ ↓H␈↓␈↓↓tack1[vv,␈↓¬NIL␈↓↓]=␈↓¬NIL␈↓↓␈↓␈α∂by␈α∂␈↓¬TACK1-NIL␈α∂␈↓and␈α∂␈↓↓¬member[W,␈↓¬NIL␈↓↓]␈↓␈α⊂by␈α∂␈↓¬MEMBER-NIL␈α∂␈↓␈α∂so␈α∂both␈α∂sides␈α⊂of␈α∂the
␈↓ ↓H␈↓equivalence are false.

␈↓ ↓H␈↓In the case ␈↓↓pl≠␈↓¬NIL␈↓↓␈↓ we let ␈↓↓pl=pll␈↓ and we have

␈↓ ↓H␈↓␈↓↓member[W,tack1[vv,pll]] ␈↓
␈↓ ↓H␈↓  ␈↓↓≡ W=[vv . ␈↓αa|␈↓↓pll] ∨ member[W,tack1[vv,␈↓αd|␈↓↓pll]]␈↓␈↓ πx␈↓¬MEMBER-UU,TACK1-PNLL,SEXP ␈↓
␈↓ ↓H␈↓  ␈↓↓≡ W=[vv . ␈↓αa|␈↓↓pll] ∨ ∃uull:[W=uull∧␈↓αa|␈↓↓uull=vv∧member[␈↓αd|␈↓↓uull,␈↓αd|␈↓↓pll] ].␈↓␈↓ λhinduction
␈↓ ↓H␈↓  ␈↓↓≡ ∃uull:[W=uull∧␈↓αa|␈↓↓uull=vv∧␈↓αd|␈↓↓uull=␈↓αa|␈↓↓pll] ∨␈↓
␈↓ ↓H␈↓  ␈↓↓  ∃uull:[W=uull∧␈↓αa|␈↓↓uull=vv∧member[␈↓αd|␈↓↓uull,␈↓αd|␈↓↓pll] ␈↓␈↓ λ_␈↓↓[vv . ␈↓αa|␈↓↓pll] ε NEUULIST␈↓
␈↓ ↓H␈↓  ␈↓↓≡ ∃uull:[W=uull∧␈↓αa|␈↓↓uull=vv∧member[␈↓αd|␈↓↓uull,pll]] ]␈↓␈↓ λ_logic of ∃ and ␈↓¬MEMBER-UU ␈↓

␈↓ ↓H␈↓Which completes the proof.

␈↓ ↓H␈↓The program ␈↓↓partn␈↓ is given by the definition

␈↓ ↓H␈↓␈↓¬PARTN: ␈↓␈↓ βX␈↓↓partn[u,n] ← ␈↓
␈↓ ↓H␈↓␈↓ ∧_␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ [␈↓αif␈↓↓ n ␈↓αequal␈↓↓ 0 ␈↓αthen␈↓↓ <␈↓¬NIL␈↓↓> ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧_␈↓↓␈↓αelse␈↓↓ [␈↓αif␈↓↓ n equal 0 ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ ∧X␈↓↓tack1[<␈↓αa|␈↓↓u>,partn[␈↓αd|␈↓↓u,sub1 n]] * tack0[␈↓αa|␈↓↓u,partn[␈↓αd|␈↓↓u,n]] ␈↓

␈↓ ↓H␈↓which is represented by the function ␈↓↓partn␈↓ satisfying

␈↓ ↓H␈↓␈↓¬PARTN-DEF: ␈↓␈↓ βX␈↓↓∀u n:partn[u,n] =␈↓
␈↓ ↓H␈↓␈↓ ∧_␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ [␈↓αif␈↓↓ n ␈↓αequal␈↓↓ 0 ␈↓αthen␈↓↓ <␈↓¬NIL␈↓↓> ␈↓αelse␈↓↓ ␈↓¬NIL␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧_␈↓↓␈↓αelse␈↓↓ [␈↓αif␈↓↓ n equal 0 ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ ∧X␈↓↓tack1[<␈↓αa|␈↓↓u>,partn[␈↓αd|␈↓↓u,sub1 n]] * tack0[␈↓αa|␈↓↓u,partn[␈↓αd|␈↓↓u,n]] ␈↓

␈↓ ↓H␈↓with the immediate corollaries

␈↓ ↓H␈↓␈↓¬PARTN-NIL-ZERO: ␈↓␈↓ βX␈↓↓partn[␈↓¬NIL␈↓↓,0] = <␈↓¬NIL␈↓↓>␈↓
␈↓ ↓H␈↓␈↓¬PARTN-NIL-NN: ␈↓␈↓ βX␈↓↓∀nn: partn[␈↓¬NIL␈↓↓,nn] = ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓¬PARTN-UU-ZERO: ␈↓␈↓ βX␈↓↓∀uu: partn[uu,0] = ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓¬PARTN-UU-NN: ␈↓␈↓ βX␈↓↓∀uu nn: partn[uu,nn] = ␈↓
␈↓ ↓H␈↓␈↓ ∧X␈↓↓tack1[<␈↓αa|␈↓↓uu>,partn[␈↓αd|␈↓↓uu,sub1 nn]] * tack0[␈↓αa|␈↓↓uu,partn[␈↓αd|␈↓↓uu,nn]] ␈↓


␈↓ ↓H␈↓The correctness of ␈↓↓partn␈↓ is stated by

␈↓ ↓H␈↓␈↓¬ISPARTN-COR: ␈↓␈↓↓∀u n:∃pl:[partn[u,n]=pl ∧ ∀W:[member[W,pl]≡Ispartn[W,u,n]]].␈↓
␈↓ ↓H␈↓␈↓ ¬wChapter  IV␈↓ *99


␈↓ ↓H␈↓We prove ␈↓¬ISPARTN-COR ␈↓by list induction.

␈↓ ↓H␈↓In␈α∞the␈α∞case␈α∂␈↓↓u=␈↓¬NIL␈↓↓␈↓␈α∞there␈α∞are␈α∂two␈α∞cases.␈α∞ If␈α∂␈↓↓n=0␈↓␈α∞then␈α∞␈↓↓partn[u,n]=<␈↓¬NIL␈↓↓>␈↓.␈α∂ Since␈α∞␈↓↓<␈↓¬NIL␈↓↓>␈↓␈α∞is␈α∂of␈α∞sort
␈↓ ↓H␈↓␈↓↓PNLIST␈↓ we need only show

␈↓ ↓H␈↓␈↓ ∧ ␈↓↓∀W:[member[W,<␈↓¬NIL␈↓↓>]≡Ispartn[W,␈↓¬NIL␈↓↓,0]]].␈↓ 

␈↓ ↓H␈↓which␈α≠follow␈α≠from␈α≠␈↓¬MEMBER-NIL,␈α≠␈↓␈↓¬MEMBER-UU,␈α≠␈↓and␈α≠␈↓¬ISPARTN-NIL-ZERO.␈α≠␈↓␈α≠If␈α≠␈↓↓n≠0␈↓␈α≠then
␈↓ ↓H␈↓␈↓↓partn[u,n]=␈↓¬NIL␈↓↓␈↓ which is also of sort ␈↓↓PNLIST,␈↓ so we need only show

␈↓ ↓H␈↓␈↓ ∧6␈↓↓∀W:[member[W,␈↓¬NIL␈↓↓]≡Ispartn[W,␈↓¬NIL␈↓↓,n]]]␈↓ 

␈↓ ↓H␈↓which follows from ␈↓¬MEMEBER-NIL, ␈↓and ␈↓¬ISPARTN-NIL-NN. ␈↓ This takes care of the case ␈↓↓u=␈↓¬NIL␈↓↓␈↓.

␈↓ ↓H␈↓In␈αthe␈αcase␈α␈↓↓u≠␈↓¬NIL␈↓↓␈↓␈αwe␈αlet␈α␈↓↓uu=u␈↓.␈α If␈α␈↓↓n=0␈↓␈αthen␈α␈↓↓partn[uu,n]=␈↓¬NIL␈↓↓␈↓␈αand␈αas␈α␈↓¬NIL␈↓␈αis␈αof␈αsort␈α␈↓↓PNLIST␈↓␈αwe
␈↓ ↓H␈↓need only show

␈↓ ↓H␈↓␈↓ ∧>␈↓↓∀W:[member[W,␈↓¬NIL␈↓↓]≡Ispartn[W,uu,0]]]␈↓ 

␈↓ ↓H␈↓which follows from ␈↓¬MEMBER-NIL ␈↓and ␈↓¬ISPARTN-UU-ZERO. ␈↓

␈↓ ↓H␈↓If ␈↓↓n≠␈↓¬NIL␈↓↓␈↓ we let ␈↓↓n=nn␈↓ and assume the induction hypothesis

␈↓ ↓H␈↓␈↓ β∧␈↓↓∀n:∃pl:[partn[␈↓αd|␈↓↓uu,n]=pl ∧ ∀W:[member[W,pl]≡Ispartn[W,␈↓αd|␈↓↓uu,n]]].␈↓ 

␈↓ ↓H␈↓Now,

␈↓ ↓H␈↓␈↓ αE␈↓↓partn[uu,nn]=tack1[<␈↓αa|␈↓↓uu>,partn[␈↓αd|␈↓↓uu,sub1 nn]] * tack0[␈↓αa|␈↓↓uu,partn[␈↓αd|␈↓↓u,nn]] ␈↓ 

␈↓ ↓H␈↓and using the induction hypothesis we have for some ␈↓↓pl␈↓ and ␈↓↓ql␈↓

␈↓ ↓H␈↓␈↓ αm␈↓↓partn[␈↓αd|␈↓↓uu,sub1 nn]=pl ∧ ∀W:[member[W,pl]≡Ispartn[W,␈↓αd|␈↓↓uu,sub1 nn]]␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ β)␈↓↓partn[␈↓αd|␈↓↓uu,nn]=ql ∧ ∀W:[member[W,ql]≡Ispartn[W,␈↓αd|␈↓↓uu,nn]]].␈↓

␈↓ ↓H␈↓By␈α
␈↓¬ISPARTN-DEF-NN␈α
␈↓we␈α∞have␈α
that␈α
every␈α∞member␈α
of␈α
␈↓↓ql␈↓␈α∞is␈α
of␈α
sort␈α∞␈↓↓NEUULIST,␈↓␈α
so␈α
we␈α∞may␈α
take
␈↓ ↓H␈↓␈↓↓ql=qnnl␈↓ to be of sort ␈↓↓PNNLIST.␈↓  Thus

␈↓ ↓H␈↓␈↓ βy␈↓↓partn[uu,nn]=tack1[<␈↓αa|␈↓↓uu>,pl] * tack0[␈↓αa|␈↓↓uu,qnnl] ␈↓ 

␈↓ ↓H␈↓which␈α∞is␈α∞of␈α
sort␈α∞␈↓↓PNLIST␈↓␈α∞using␈α
function␈α∞mappings␈α∞for␈α
␈↓↓tack1,␈↓␈α∞␈↓↓tack0␈↓␈α∞and␈α
␈↓↓append.␈↓␈α∞ So␈α∞we␈α
need
␈↓ ↓H␈↓only show

␈↓ ↓H␈↓␈↓ βt␈↓↓∀W:[member[W,partn[uu,nn]]≡Ispartn[W,uu,nn]]]␈↓ 

␈↓ ↓H␈↓to complete the proof.  This follows by the following argument:
␈↓ ↓H␈↓100␈↓ ¬wChapter  IV␈↓ H



␈↓ ↓H␈↓␈↓↓member[W,partn[uu,nn]]␈↓
␈↓ ↓H␈↓␈↓ ↓X␈↓↓≡member[W,tack1[<␈↓αa|␈↓↓uu>,pl]] ∨ member[W,tack0[␈↓αa|␈↓↓uu,qnnl]]␈↓␈↓ λ8;by ␈↓¬MEMBER-UV ␈↓
␈↓ ↓H␈↓␈↓ ↓X␈↓↓≡[∃uull:[W=uull∧␈↓αa|␈↓↓uull=<␈↓αa|␈↓↓uu>∧member[␈↓αd|␈↓↓uull,pl]] ∨␈↓
␈↓ ↓H␈↓␈↓ ↓X␈↓↓  ∃uull:[W=uull∧␈↓αaa|␈↓↓uull=␈↓αa|␈↓↓uu∧member[␈↓αda|␈↓↓uull . ␈↓αd|␈↓↓uull,qnnl]] ]␈↓␈↓ λ8;by ␈↓¬MEM-TACK1,MEM-TACK0 ␈↓
␈↓ ↓H␈↓␈↓ ↓X␈↓↓≡[∃uull:[W=uull∧␈↓αa|␈↓↓uull=<␈↓αa|␈↓↓uu>∧Ispartn[␈↓αd|␈↓↓uull,␈↓αd|␈↓↓uu,sub1 nn]] ∨␈↓
␈↓ ↓H␈↓␈↓ ↓X␈↓↓  ∃uull:[W=uull∧␈↓αaa|␈↓↓uull=␈↓αa|␈↓↓uu∧Ispartn[␈↓αda|␈↓↓uull . ␈↓αd|␈↓↓uull,␈↓αd|␈↓↓uu,nn]] ]␈↓␈↓ λ8;by induction hypothesis
␈↓ ↓H␈↓␈↓ ↓x␈↓↓≡Ispartn[W,uu,nn]]␈↓␈↓ λ8;by ␈↓¬ISPARTN-UU-NN ␈↓


␈↓ ↓H␈↓This␈α
completes␈α
the␈α
proof.␈α
 Note␈α
that␈α
we␈α
did␈α
not␈αprove␈α
␈↓↓partn␈↓␈α
to␈α
be␈α
total␈α
as␈α
a␈α
separate␈α
step.␈α This␈α
is
␈↓ ↓H␈↓because the clause ␈↓↓∃pl.partn[u,n]=pl␈↓ says among other things that ␈↓↓PNLIST partn[u,n]␈↓.

␈↓ ↓H␈↓α␈↓ β"Formal description of domains ␈↓↓UULIST,PNLIST,PNNLIST␈↓α

␈↓ ↓H␈↓Thus we have the subdomain relations:

␈↓ ↓H␈↓␈↓ βx␈↓↓UULIST = NULL ∪ NEUULIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓NULL ∩ NEUULIST = ␈↓πf␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓UULIST ⊂ LIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓NEUULIST ⊂ NELIST␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓PNLIST = NULL ∪ NEPNLIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓NULL ∩ NEPNLIST = ␈↓πf␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓PNLIST ⊂ LIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓NEPNLIST ⊂ NELIST␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓PNNLIST = NULL ∪ NEPNNLIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓NULL ∩ NEPNNLIST = ␈↓πf␈↓↓␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓PNNLIST ⊂ UULIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓NEPNNLIST ⊂ NEUULIST␈↓

␈↓ ↓H␈↓We will have additional variables ranging over the new sorts.

␈↓ ↓H␈↓␈↓ βx␈↓↓uul, vvl, wwl ε UULIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓uull, vvll, wwll ε NEUULIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓pl, ql, rl ε PNLIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓pll, qll, rll ε NEPNLIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓pnnl, qnnl, rnnl ε PNNLIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓pnnll, qnnll, rnnll ε NEPNNLIST␈↓

␈↓ ↓H␈↓The function mappings for ␈↓↓car,␈↓ ␈↓↓cdr,␈↓ and ␈↓↓cons␈↓ on the new sorts is as follows:

␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αa␈↓↓: NEUULIST → NELIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αd␈↓↓: NELIST → LIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αcons␈↓↓: NELIST ␈↓¬x␈↓↓ UULIST → NEUULIST␈↓

␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αa␈↓↓: NEPNLIST → UULIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αd␈↓↓: NEPNLIST → PNLIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αcons␈↓↓: UULIST ␈↓¬x␈↓↓ PNLIST → NEPNLIST␈↓
␈↓ ↓H␈↓␈↓ ¬wChapter  IV␈↓ ≠101



␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αa␈↓↓: NEPNNLIST → NEUULIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αd␈↓↓: NEPNNLIST → PNNLIST␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αcons␈↓↓: NEUULIST ␈↓¬x␈↓↓ PNNLIST → NEPNNLIST␈↓

␈↓ ↓H␈↓Since␈α∞␈↓↓UULIST␈↓s␈α∂␈↓↓PNLISTS␈↓s␈α∞and␈α∞␈↓↓PNNLIST␈↓s␈α∂are␈α∞analogous␈α∞to␈α∂␈↓↓LIST␈↓s␈α∞Thus␈α∞we␈α∂have␈α∞recursion
␈↓ ↓H␈↓and induction on these domains.  In particular we have the induction schemata:

␈↓ ↓H␈↓␈↓ α_␈↓¬UULISTIND: ␈↓  ␈↓↓␈↓πF␈↓↓ ␈↓¬NIL␈↓↓∧∀uull:[␈↓πF␈↓↓ ␈↓αd|␈↓↓uull ⊃ ␈↓πF␈↓↓ uull] ⊃ ∀uul:␈↓πF␈↓↓ uul␈↓
␈↓ ↓H␈↓␈↓ α_␈↓¬PNLISTIND: ␈↓   ␈↓↓␈↓πF␈↓↓ ␈↓¬NIL␈↓↓ ∧ ∀pll:[␈↓πF␈↓↓ ␈↓αd|␈↓↓pll ⊃ ␈↓πF␈↓↓ pll] ⊃ ∀pl:␈↓πF␈↓↓ pl ␈↓
␈↓ ↓H␈↓␈↓ α_␈↓¬PNNLISTIND: ␈↓  ␈↓↓␈↓πF␈↓↓ ␈↓¬NIL␈↓↓ ∧ ∀pnnll:[␈↓πF␈↓↓ ␈↓αd|␈↓↓pnnll ⊃ ␈↓πF␈↓↓ pnnll] ⊃ ∀pnnl:␈↓πF␈↓↓ pnnl .␈↓



␈↓ ↓H␈↓3.  ␈↓αExercises ␈↓


␈↓ ↓H␈↓        This␈α∂collection␈α∞of␈α∂exercises␈α∂treats␈α∞properties␈α∂of␈α∂association␈α∞lists,␈α∂substitutions,␈α∂and␈α∞pattern
␈↓ ↓H␈↓matching.␈α∪ These␈α∩topics␈α∪are␈α∪strongly␈α∩interrelated␈α∪and␈α∩the␈α∪ability␈α∪to␈α∩treat␈α∪properties␈α∪of␈α∩such
␈↓ ↓H␈↓programs is fundamental to the general area of symbolic computation.

␈↓ ↓H␈↓1. Association lists

␈↓ ↓H␈↓        Prove the following facts about association lists.

␈↓ ↓H␈↓        1.1.     ␈↓↓∀u v: [alist u ∧ alist v ⊃ alist[u * v]␈↓

␈↓ ↓H␈↓        1.2.     ␈↓↓∀z u v: alist u ∧ alist v ⊃ ␈↓
␈↓ ↓H␈↓                          ␈↓↓[assoc[z,u*v] = ␈↓αif␈↓↓ ␈↓αn|␈↓↓assoc[z,u] ␈↓αthen␈↓↓ assoc[z,v] ␈↓αelse␈↓↓ assoc[z,u]]␈↓

␈↓ ↓H␈↓where

␈↓ ↓H␈↓        ␈↓↓alist u ← ␈↓αn|␈↓↓u ∨ [¬␈↓αat|␈↓↓␈↓αa|␈↓↓u ∧ alist ␈↓αd|␈↓↓u]]]␈↓

␈↓ ↓H␈↓        ␈↓↓assoc[x, s] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓s ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ x = ␈↓αaa|␈↓↓s ␈↓αthen␈↓↓ ␈↓αa|␈↓↓s ␈↓αelse␈↓↓ assoc[x, ␈↓αd|␈↓↓s]␈↓

␈↓ ↓H␈↓2. Properties of substitutions and substitution lists.

␈↓ ↓H␈↓        With␈α∩function␈α∪definitions␈α∩as␈α∩given␈α∪below,␈α∩␈↓↓subst[x, y, z]␈↓␈α∩is␈α∪the␈α∩result␈α∩of␈α∪replacing␈α∩the
␈↓ ↓H␈↓variable␈α␈↓↓y␈↓␈αby␈αthe␈α
S-expression␈α␈↓↓x␈↓␈αwherever␈α␈↓↓y␈↓␈α
occurs␈αin␈α␈↓↓z.␈↓␈α If␈α
␈↓↓s␈↓␈αis␈αa␈αlist␈α
of␈αsubstitutions␈αof␈αthe␈α
form
␈↓ ↓H␈↓␈↓↓y . x␈↓␈α
then␈α
␈↓↓sublis[z, s]␈↓␈αis␈α
the␈α
result␈α
of␈α"simultaneously"␈α
performing␈α
all␈αof␈α
the␈α
substitutions␈α
on␈αthe
␈↓ ↓H␈↓list␈α
to␈α␈↓↓z.␈↓␈α
If␈α
␈↓↓s1␈↓␈αand␈α
␈↓↓s2␈↓␈α
are␈αlists␈α
of␈α
substitutions␈αthen␈α
␈↓↓s1␈↓␈α
@␈α␈↓↓s2␈↓␈α
is␈α
the␈α"composition"␈α
 of␈α
the␈αtwo.␈α
 Prove
␈↓ ↓H␈↓the following properties.

␈↓ ↓H␈↓        2.1.     ␈↓↓∀x y z: subst[x, y, z] = sublis[z, <y . x>]␈↓

␈↓ ↓H␈↓        2.2.     ␈↓↓∀z s1 s2: sublis[z, s1 @ s2] = sublis[sublis[z, s1], s2]␈↓

␈↓ ↓H␈↓        2.3.     ␈↓↓∀z s1 s2 s3: sublis[z, s1 @ [s2 @ s3]] = sublis[z, [s1 @ s2] @ s3]␈↓

␈↓ ↓H␈↓        2.4.     ␈↓↓∀x x1 y y1 z: ((y ≠  y1 ∧ ¬occur[y, x1]) ⊃ ␈↓
␈↓ ↓H␈↓102␈↓ ¬wChapter  IV␈↓ H


␈↓ ↓H␈↓                        ␈↓↓subst[x1, y1, subst[x, y, z]] = subst[subst[x1, y1, x], y, subst[x1, y1, z]])␈↓


␈↓ ↓H␈↓where

␈↓ ↓H␈↓        ␈↓↓subst[x, y, z] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓z ␈↓αthen␈↓↓ [␈↓αif␈↓↓ y = z ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ z] ␈↓
␈↓ ↓H␈↓                        ␈↓↓␈↓αelse␈↓↓ subst[x, y, ␈↓αa|␈↓↓z] . subst[x, y, ␈↓αd|␈↓↓z]␈↓

␈↓ ↓H␈↓        ␈↓↓sublis[x, s] ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ {assoc[x, s]}[λz: ␈↓αif␈↓↓ ␈↓αn|␈↓↓z ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ ␈↓αd|␈↓↓z]␈↓
␈↓ ↓H␈↓                        ␈↓↓␈↓αelse␈↓↓ sublis[␈↓αa|␈↓↓x, s] . sublis[␈↓αd|␈↓↓x, s]␈↓

␈↓ ↓H␈↓        ␈↓↓s1 @ s2 ← subsub[s1, s2] * s2␈↓

␈↓ ↓H␈↓        ␈↓↓subsub[s1, s2] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓s1 ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ [␈↓αaa|␈↓↓s1 . sublis[␈↓αda|␈↓↓s1, s2]] . subsub[␈↓αd|␈↓↓s1, s2]␈↓

␈↓ ↓H␈↓3.  Pattern matching

␈↓ ↓H␈↓        The␈α∞function␈α∞␈↓↓inst␈↓␈α∞is␈α
one␈α∞sort␈α∞of␈α∞"pattern␈α
matcher".␈α∞ We␈α∞say␈α∞that␈α
an␈α∞S-expression␈α∞␈↓↓x␈↓␈α∞is␈α
an
␈↓ ↓H␈↓instance␈αof␈αanother␈αS-expression␈α␈↓↓y␈↓␈αif␈α␈↓↓y␈↓␈αcan␈αbe␈αtransformed␈αinto␈α␈↓↓x␈↓␈αby␈αreplacing␈αsome␈αof␈αthe␈αatoms
␈↓ ↓H␈↓of␈α⊃␈↓↓y␈↓␈α⊂which␈α⊃satisfy␈α⊂␈↓↓isvar␈↓␈α⊃by␈α⊂suitable␈α⊃values.␈α⊂ (All␈α⊃occurrences␈α⊂of␈α⊃the␈α⊂same␈α⊃variable␈α⊃should␈α⊂be
␈↓ ↓H␈↓replaced␈αby␈αthe␈αsame␈αvalue.)␈α ␈↓↓inst[x, y, ␈↓¬NIL␈↓↓]␈↓␈αis␈α␈↓¬NO␈α␈↓if␈α␈↓↓x␈↓␈αis␈αnot␈αand␈αinstance␈αof␈αthe␈αpattern␈α␈↓↓y,␈↓␈αand
␈↓ ↓H␈↓otherwise␈α∩is␈α⊃the␈α∩list␈α⊃of␈α∩substitions␈α∩which␈α⊃will␈α∩convert␈α⊃␈↓↓y␈↓␈α∩into␈α∩␈↓↓x.␈↓␈α⊃ In␈α∩the␈α⊃latter␈α∩case␈α∩we␈α⊃have
␈↓ ↓H␈↓␈↓↓x = sublis[y, inst[x, y, ␈↓¬NIL␈↓↓]␈↓.  Write a definition for ␈↓↓inst␈↓ and prove that

␈↓ ↓H␈↓␈↓ βQ␈↓↓∀x y u: (inst[x, y, u] ≠ ␈↓¬NO ␈↓↓⊃ x = sublis[y, inst[x, y, u]])␈↓ 

␈↓ ↓H␈↓4. Unification

␈↓ ↓H␈↓        (This is a fairly substantial exercise.)

␈↓ ↓H␈↓        ␈↓↓unify[x,y]␈↓␈αattempts␈αto␈αfind␈αthe␈αmost␈αgeneral␈αpattern␈αwhich␈αis␈αan␈αinstance␈αof␈αboth␈α␈↓↓x␈↓␈αand␈α␈↓↓y.␈↓
␈↓ ↓H␈↓If␈α
no␈α
such␈αpattern␈α
exists␈α
the␈αit␈α
returns␈α
␈↓¬NO,␈α
␈↓otherwise␈αit␈α
returns␈α
a␈αlist␈α
of␈α
substitutions␈α
which␈αwill
␈↓ ↓H␈↓convert both ␈↓↓x␈↓ and ␈↓↓y␈↓ into that pattern.  Write a definition of ␈↓↓unify␈↓ and prove

␈↓ ↓H␈↓        4.1.     ␈↓↓∀x y: (unify[x, y] ≠ ␈↓¬NO ␈↓↓⊃ sublis[x, unify[x, y]] = sublis[y, unify[x, y]])␈↓

␈↓ ↓H␈↓        4.2.     ␈↓↓∀x y: (unify[x, y] = ␈↓¬NO ␈↓↓⊃ ∀s: sublis[x, s] ≠ sublis[y, s])␈↓

␈↓ ↓H␈↓        4.3.     ␈↓↓∀x y s: (sublis[x, s] = sublis[y, s] ⊃ ∃s1: ∀z: sublis[z, s] = sublis[z, unify[x, y] @ s1])␈↓
␈↓ ↓H␈↓␈↓ εH␈↓ ≠103


␈↓ ↓H␈↓α␈↓ εαChapter V

␈↓ ↓H␈↓α␈↓ ∧%LISP PROGRAMS WITH SIDE EFFECTS


␈↓ ↓H␈↓        Pure␈αclean␈αLISP␈αprograms␈αsuch␈αas␈αwe␈αdescribed␈αin␈αChapters␈αI␈αand␈αII␈αsimply␈αcompute␈αsome
␈↓ ↓H␈↓function␈α
of␈α
their␈αarguments␈α
and␈α
have␈αno␈α
effect␈α
on␈αthe␈α
environment␈α
in␈αwhich␈α
they␈α
are␈αexecuted.
␈↓ ↓H␈↓They␈α∩admit␈α∩the␈α⊃elegant␈α∩mathematical␈α∩characterization␈α∩described␈α⊃and␈α∩applied␈α∩in␈α∩Chapter␈α⊃III.
␈↓ ↓H␈↓Specifically,␈αeach␈αrecursively␈α
defined␈αpure␈αclean␈α
LISP␈αprogram␈αcan␈α
be␈αtranslated␈αinto␈αa␈α
functional
␈↓ ↓H␈↓equation␈α
and␈αminimization␈α
schema␈αin␈α
a␈α
first␈αorder␈α
language,␈αand␈α
the␈α
equation␈αand␈α
schema␈αcan␈α
be
␈↓ ↓H␈↓used␈α∀to␈α∃prove␈α∀that␈α∃the␈α∀program␈α∀meets␈α∃its␈α∀extensional␈α∃specifications␈α∀or␈α∃other␈α∀mathematical
␈↓ ↓H␈↓properties of the function computed by the program.

␈↓ ↓H␈↓        However␈α⊃this␈α∩is␈α⊃not␈α∩the␈α⊃whole␈α∩story.␈α⊃ LISP␈α⊃systems␈α∩also␈α⊃provide␈α∩for␈α⊃the␈α∩definition␈α⊃of
␈↓ ↓H␈↓sequential␈α∂programs␈α∂in␈α⊂addition␈α∂to␈α∂the␈α∂recursive␈α⊂definition␈α∂mechanism.␈α∂ A␈α⊂sequential␈α∂program
␈↓ ↓H␈↓operates␈αby␈αperforming␈αa␈αsequence␈αof␈αoperations␈αmodifying␈αthe␈α"state".␈α   Thus␈αLISP␈αsystems␈αalso
␈↓ ↓H␈↓provide␈α
a␈α
variety␈α
of␈α
features␈α
for␈α
examining␈α∞and␈α
modifying␈α
the␈α
state␈α
of␈α
the␈α
LISP␈α∞world.␈α
 These
␈↓ ↓H␈↓include␈α⊂assigning␈α⊂values␈α⊃to␈α⊂variables,␈α⊂moregenerally␈α⊂modifying␈α⊃the␈α⊂"property␈α⊂list"␈α⊂of␈α⊃an␈α⊂atom,
␈↓ ↓H␈↓destructive␈α
(as␈α
opposed␈α
to␈α
␈↓↓cons␈↓tructive)␈αoperations␈α
on␈α
list␈α
structure,␈α
even␈α
examining␈αand␈α
modifying
␈↓ ↓H␈↓the LISP control mechanisms.

␈↓ ↓H␈↓A␈α
sequential␈α
program␈αcan␈α
be␈α
viewed␈α
as␈αcomputing␈α
a␈α
function␈α
by␈αdesignating␈α
some␈α
property␈αof␈α
the
␈↓ ↓H␈↓final␈α
state␈α
of␈α
the␈α
computation␈α
(such␈α
as␈α
the␈αvalue␈α
of␈α
some␈α
variable)␈α
as␈α
the␈α
output.␈α
  In␈α
general␈αa
␈↓ ↓H␈↓LISP␈α∂program␈α∂will␈α∂be␈α⊂used␈α∂both␈α∂to␈α∂compute␈α∂a␈α⊂function␈α∂and␈α∂to␈α∂cause␈α∂"side-effects".␈α⊂ By␈α∂using
␈↓ ↓H␈↓programs␈αwith␈αside-effects␈αin␈αa␈αlarger␈αprogram␈αone␈αcan␈αoften␈αcompute␈αanswers␈αmore␈αconveniently
␈↓ ↓H␈↓than␈α∞in␈α∞pure␈α∞LISP.␈α∞ Side-effects␈α∞can␈α∞be␈α∞used␈α∞as␈α∞a␈α∞means␈α∞of␈α∞communication␈α∞among␈α∞a␈α∂group␈α∞of
␈↓ ↓H␈↓programs acting jointly to carry out a computation.

␈↓ ↓H␈↓        The␈αmathematical␈αproperties␈αof␈αgeneral␈αside-effects␈αare␈αnot␈αwell␈αunderstood,␈αalthough␈αsome
␈↓ ↓H␈↓techniques␈α↔for␈α⊗treating␈α↔certain␈α⊗classes␈α↔of␈α⊗"well-structured"␈α↔sequential␈α⊗programs␈α↔have␈α⊗been
␈↓ ↓H␈↓developed.␈α
 Programs␈α
that␈αtake␈α
totally␈α
uninhibited␈α
advantage␈αof␈α
side-effects␈α
are␈α
usually␈αdifficult
␈↓ ↓H␈↓to␈αunderstand,␈αdebug␈αand␈α
modify.␈α This␈αhas␈αled␈αto␈α
experienced␈αprogrammers␈αto␈αuse␈αthese␈α
features
␈↓ ↓H␈↓in␈αmoderation.␈α That␈αsuch␈αprograms␈αare␈αnot␈α
accessible␈αto␈αpresent␈αproof␈αtechniques␈αis␈αof␈α
secondary
␈↓ ↓H␈↓interest␈α
to␈αmost␈α
applied␈αprogrammers.␈α
 In␈αthe␈α
future␈α
we␈αexpect␈α
that␈αlanguage␈α
designers␈αwill␈α
reduce
␈↓ ↓H␈↓the␈αneed␈αfor␈αside-effect␈αprogramming␈αby␈αproviding␈αmore␈αdirect␈αmeans␈αfor␈αachieving␈αtheir␈αresults,
␈↓ ↓H␈↓and␈α∂programmers␈α∂will␈α∂come␈α∂to␈α∂see␈α∂debugging␈α∞a␈α∂correctness␈α∂proof␈α∂as␈α∂a␈α∂more␈α∂worthwile␈α∞activity
␈↓ ↓H␈↓than␈α∞debugging␈α∞a␈α∞program␈α∞by␈α∞examples,␈α∞because␈α
it␈α∞terminates␈α∞with␈α∞a␈α∞conclusive␈α∞proof␈α∞that␈α
the
␈↓ ↓H␈↓program meets its specifications.

␈↓ ↓H␈↓        In␈αthis␈αchapter␈αwe␈αshall␈αdescribe␈αsequential␈αprograms␈αin␈αLISP␈αand␈αsome␈αadditional␈αfeatures
␈↓ ↓H␈↓for␈αside-effect␈αprogramming␈αsuch␈αas␈αarrays,␈αproperty␈αlist␈αoperations,␈αand␈αdestructive␈αlist␈αstructure
␈↓ ↓H␈↓operations.␈α
 Every␈α∞computation␈α
that␈α∞can␈α
be␈α
done␈α∞with␈α
these␈α∞features␈α
can␈α
be␈α∞done␈α
in␈α∞pure␈α
clean
␈↓ ↓H␈↓LISP,␈α
but␈α
nevertheless␈α
there␈α
are␈α
often␈α
good␈α
reasons␈α
for␈α
using␈α
these␈α
features.␈α
 We␈α
shall␈α
examine
␈↓ ↓H␈↓the␈α∪features␈α∀themselves␈α∪and␈α∀also␈α∪the␈α∀criteria␈α∪that␈α∪determine␈α∀when␈α∪they␈α∀should␈α∪be␈α∀used␈α∪in
␈↓ ↓H␈↓preference␈α⊂to␈α⊂pure␈α⊂clean␈α⊂LISP.␈α⊂ Ideas␈α⊂for␈α⊂mathematical␈α⊂treatment␈α⊂of␈α⊂sequential␈α⊃programs␈α⊂and
␈↓ ↓H␈↓side-effects␈α⊂will␈α⊂be␈α⊂presented␈α⊃in␈α⊂a␈α⊂later␈α⊂chapter.␈α⊂ We␈α⊃also␈α⊂postpone␈α⊂discussion␈α⊂of␈α⊃features␈α⊂for
␈↓ ↓H␈↓examing and modifying the LISP control structure until Chapter VI.
␈↓ ↓H␈↓104␈↓ ¬|Chapter  V␈↓ H


␈↓ ↓H␈↓1.  ␈↓αSequential (ALGOL-like) programs.␈↓


␈↓ ↓H␈↓        Sequential␈α∪programs␈α∪are␈α∀impure␈α∪(by␈α∪definition),␈α∪but␈α∀can␈α∪be␈α∪clean␈α∪-␈α∀provided␈α∪certain
␈↓ ↓H␈↓restrictions␈αare␈αobserved.␈α The␈αexternal␈αnotation␈αfor␈αsequential␈αprograms␈αis␈αadapted␈αfrom␈αthat␈αof
␈↓ ↓H␈↓ALGOL 60.

␈↓ ↓H␈↓        As an example, we might write ␈↓↓reverse␈↓ as follows:

␈↓ ↓H␈↓↓␈↓ βxreverse u ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[v]
␈↓ ↓H␈↓↓␈↓ ∧xv ← ␈↓¬NIL␈↓↓;
␈↓ ↓H␈↓↓1.1)␈↓ ∧8a:␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ v;
␈↓ ↓H␈↓↓␈↓ ∧xv ← ␈↓αa|␈↓↓u . v;
␈↓ ↓H␈↓↓␈↓ ∧xu ← ␈↓αd|␈↓↓u;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ a].

␈↓ ↓H␈↓        The internal form of the same program is

␈↓ ↓H␈↓¬␈↓ βx(DEFUN REVERSE (U)
␈↓ ↓H␈↓¬␈↓ ∧8(PROG (V)
␈↓ ↓H␈↓¬␈↓ ∧x(SETQ V NIL)
␈↓ ↓H␈↓¬␈↓ ∧xA
␈↓ ↓H␈↓¬␈↓ ∧x(COND ((NULL U) (RETURN V)))
␈↓ ↓H␈↓¬␈↓ ∧x(SETQ V (CONS (CAR U) V))
␈↓ ↓H␈↓¬␈↓ ∧x(SETQ U (CDR U))
␈↓ ↓H␈↓¬␈↓ ∧x(GO A)))

␈↓ ↓H␈↓where the paragraphing is only for the reader's convenience.

␈↓ ↓H␈↓        Sequential␈α∞programs␈α∂are␈α∞introduced␈α∞in␈α∂LISP␈α∞by␈α∞allowing␈α∂as␈α∞a␈α∞term␈α∂an␈α∞expression␈α∂of␈α∞the
␈↓ ↓H␈↓(external) form

␈↓ ↓H␈↓␈↓ βx␈↓αprogram␈↓[[<variable list>] <statement list>],

␈↓ ↓H␈↓where␈α
<variable␈αlist>␈α
is␈αa␈α
list␈αof␈α
variables␈αlocal␈α
to␈αthe␈α
program,␈αand␈α
<statement␈αlist>␈α
is␈αa␈α
list␈αof␈α
the
␈↓ ↓H␈↓statements␈α
of␈α
the␈αprogram.␈α
 In␈α
external␈α
notation,␈αas␈α
in␈α
ALGOL␈α60,␈α
the␈α
statements␈α
are␈αseparated
␈↓ ↓H␈↓by semicolons, and any statement may be preceded by a label followed by a colon.

␈↓ ↓H␈↓        The statements are of the following kinds:

␈↓ ↓H␈↓        1. Assignment statements of the form

␈↓ ↓H␈↓␈↓ βx<left hand side> ← <right hand side>,

␈↓ ↓H␈↓where␈α⊃<left␈α⊂hand␈α⊃side>␈α⊃is␈α⊂a␈α⊃variable,␈α⊂possibly␈α⊃subscripted,␈α⊃and␈α⊂<right␈α⊃hand␈α⊂side>␈α⊃is␈α⊃a␈α⊂LISP
␈↓ ↓H␈↓expression that can be evaluated.

␈↓ ↓H␈↓        2. ␈↓αgo to␈↓ statements of the form

␈↓ ↓H␈↓␈↓ βx␈↓αgo to␈↓ <label>
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠105


␈↓ ↓H␈↓where␈α⊂<label>␈α⊂is␈α⊂ an␈α⊂expression␈α⊂that␈α⊂evaluates␈α∂to␈α⊂a␈α⊂label.␈α⊂  Since␈α⊂labels␈α⊂are␈α⊂atoms␈α⊂in␈α∂internal
␈↓ ↓H␈↓notation,␈α
any␈α
expression␈α
evaluating␈α
to␈α
an␈α
atom␈α
may␈αbe␈α
used,␈α
but␈α
the␈α
usual␈α
case␈α
is␈α
a␈αconditional
␈↓ ↓H␈↓expression␈αwherein␈α
the␈αsecond␈α
element␈αof␈α
each␈αpair␈αis␈α
an␈αatom.␈α
 Should␈αthe␈α
resulting␈αexpression
␈↓ ↓H␈↓not be an atom or should that atom not be used as a label, an error message will be generated.

␈↓ ↓H␈↓        3. Conditional statements which have the form

␈↓ ↓H␈↓␈↓ βx␈↓αif␈↓ <p1> ␈↓αthen␈↓ <s1> ␈↓αelse␈↓ ␈↓αif␈↓ ... ␈↓αelse␈↓ ␈↓αif␈↓ <pn> ␈↓αthen␈↓ <sn>,

␈↓ ↓H␈↓where␈α⊂the␈α⊂<pi>␈α∂are␈α⊂propositional␈α⊂terms␈α⊂having␈α∂truth␈α⊂values,␈α⊂and␈α∂the␈α⊂<si>␈α⊂are␈α⊂any␈α∂statements.
␈↓ ↓H␈↓Notice␈α
that␈α
conditional␈α
statements␈α∞terminate␈α
with␈α
a␈α
␈↓αthen␈↓␈α∞clause␈α
as␈α
do␈α
conditional␈α∞expressions␈α
in
␈↓ ↓H␈↓general␈α
in␈α
internal␈α
form␈α
If␈α
none␈α
of␈α
the␈α
propositional␈α
terms␈α
are␈α
true␈α
the␈α
statement␈α
has␈α∞no␈α
effect.
␈↓ ↓H␈↓(Unless of course there were side effects in the evaluation of the propositional terms.)

␈↓ ↓H␈↓        4. ␈↓αreturn␈↓ statements of the form

␈↓ ↓H␈↓␈↓ βx␈↓αreturn␈↓ <expression>

␈↓ ↓H␈↓where␈α<expression>␈αis␈αan␈αarbitrary␈αLISP␈αterm.␈α The␈αeffect␈αof␈αexecuting␈αthis␈αstatement␈αis␈αto␈αreturn
␈↓ ↓H␈↓from the program giving the program as a term the value of <expression>.

␈↓ ↓H␈↓        In general the internal form of a program term is the following:

␈↓ ↓H␈↓␈↓ ∧b(␈↓¬PROG ␈↓ <variable list> <s1> ... <sn>)

␈↓ ↓H␈↓and the four types of statements have the forms:

␈↓ ↓H␈↓␈↓ βx(␈↓¬SETQ ␈↓<variable> <expression>)
␈↓ ↓H␈↓␈↓ βx(␈↓¬GO ␈↓<label>)
␈↓ ↓H␈↓␈↓ βx(␈↓¬COND ␈↓(<p1> <s1>) ... (<pn> <sn>))
␈↓ ↓H␈↓␈↓ βx(␈↓¬RETURN ␈↓<expression>)


␈↓ ↓H␈↓In␈α⊃addition␈α⊃a␈α⊂␈↓¬PROG␈α⊃␈↓statement␈α⊃may␈α⊂be␈α⊃an␈α⊃atom␈α⊂which␈α⊃is␈α⊃interpreted␈α⊂as␈α⊃a␈α⊃label.␈α⊃ (␈↓¬GO␈α⊂␈↓<label>)
␈↓ ↓H␈↓transfers␈α∞control␈α∞to␈α∞the␈α∞list␈α∞of␈α
statements␈α∞beginning␈α∞with␈α∞the␈α∞statement␈α∞following␈α∞<label>.␈α
 Thus
␈↓ ↓H␈↓internal␈α
notation␈α∞is␈α
again␈α∞more␈α
uniform␈α∞than␈α
external␈α
notation␈α∞in␈α
treating␈α∞labels␈α
as␈α∞elements␈α
of
␈↓ ↓H␈↓the statement list rather than as attachments to statements.

␈↓ ↓H␈↓        In␈α
some␈αLISP␈α
implementations␈αarrays␈α
are␈α
treated␈αspecially␈α
and␈αsubscripted␈α
variables␈αare␈α
not
␈↓ ↓H␈↓allowed␈αas␈α
first␈αarguments␈αto␈α
␈↓¬SETQ.␈α␈↓␈αThe␈α
␈↓¬STORE␈α␈↓command␈αmust␈α
be␈αused␈α in␈α
order␈αto␈α
change␈αan
␈↓ ↓H␈↓array element.  We will say more about arrays in the next section.

␈↓ ↓H␈↓        As␈α
further␈α
examples␈α
of␈α
how␈α
␈↓αprogram␈↓␈α
might␈α∞be␈α
used,␈α
here␈α
are␈α
some␈α
ways␈α
we␈α∞might␈α
write
␈↓ ↓H␈↓␈↓↓append:␈↓

␈↓ ↓H␈↓1.2)␈↓ βA␈↓↓u * v ← ␈↓αprogram␈↓↓[[] ␈↓αreturn␈↓↓ ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v]]␈↓, 

␈↓ ↓H␈↓is just a trivial rewrite of the recursive definition.
␈↓ ↓H␈↓106␈↓ ¬|Chapter  V␈↓ H



␈↓ ↓H␈↓↓␈↓ βxu * v ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[w]
␈↓ ↓H␈↓↓1.3)␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ v;
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓αa|␈↓↓u . [␈↓αd|␈↓↓u * v];
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αreturn␈↓↓ w]


␈↓ ↓H␈↓is␈αalmost␈α
as␈αclose␈α
to␈αthe␈α
pure␈αLISP␈α
form.␈α If␈αwe␈α
want␈αto␈α
replace␈αthe␈α
recursion␈αby␈α
a␈αloop,␈α
we␈αcan
␈↓ ↓H␈↓write

␈↓ ↓H␈↓↓␈↓ βxu * v ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[u1,v1,w]
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓¬NIL␈↓↓;
␈↓ ↓H␈↓↓␈↓ ∧xu1 ← u;
␈↓ ↓H␈↓↓␈↓ ∧xv1 ← v;
␈↓ ↓H␈↓↓␈↓ ∧8a:␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓u1 ␈↓αthen␈↓↓ ␈↓αgo to␈↓↓ b;
␈↓ ↓H␈↓↓1.4)␈↓ ∧xw ← ␈↓αa|␈↓↓u1 . w;
␈↓ ↓H␈↓↓␈↓ ∧xu1 ← ␈↓αd|␈↓↓u;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ a;
␈↓ ↓H␈↓↓␈↓ ∧8b:␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ v1;
␈↓ ↓H␈↓↓␈↓ ∧xv1 ← ␈↓αa|␈↓↓w . v1;
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓αd|␈↓↓w;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ b].


␈↓ ↓H␈↓This corresponds to the recursive program


␈↓ ↓H␈↓↓␈↓ βxu * v ← app[u,v,␈↓¬NIL␈↓↓]
␈↓ ↓H␈↓↓1.5)
␈↓ ↓H␈↓↓␈↓ βxapp[u,v,w] ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ [␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ app[u,␈↓αa|␈↓↓w . v,␈↓αd|␈↓↓w]]
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ app[␈↓αd|␈↓↓u,v,␈↓αa|␈↓↓u . w]


␈↓ ↓H␈↓which␈αwould␈α
have␈αessentially␈αthe␈α
same␈α"computation␈αsequence"␈α
as␈αthe␈αsequential␈α
program␈αif␈α
it␈αis
␈↓ ↓H␈↓compiled or interpreted iteratively (without using the stack).

␈↓ ↓H␈↓        As␈αa␈αfinal␈αexample␈αwe␈αhave␈αa␈αsequential␈αprogram␈αfor␈αcomputing␈αthe␈αpartitions␈αof␈αa␈αlist.␈α A
␈↓ ↓H␈↓recursive␈α∂program␈α⊂was␈α∂given␈α∂and␈α⊂proved␈α∂correct␈α⊂in␈α∂Chapter␈α∂III.␈α⊂ This␈α∂example␈α⊂illustrates␈α∂the
␈↓ ↓H␈↓difference␈αbetween␈α"top-down"␈α(recursive)␈αand␈α
"bottom-up"␈α(sequential)␈αthinking.␈α  Recall␈αthat␈α
the
␈↓ ↓H␈↓criterion for ␈↓↓W␈↓ to be a partition of a list ␈↓↓u␈↓ into ␈↓↓n␈↓ parts is

␈↓ ↓H␈↓␈↓ αX␈↓↓∀W u n.(Ispartn[W,u,n] ≡ ∃uul.(W=uul ∧ length uul=n ∧ combine uul=u)).␈↓ 

␈↓ ↓H␈↓To␈α⊂write␈α⊂the␈α⊂recursive␈α∂program␈α⊂␈↓↓partn[u,n]␈↓␈α⊂we␈α⊂listed␈α⊂the␈α∂partitions␈α⊂for␈α⊂that␈α⊂base␈α⊂cases␈α∂where
␈↓ ↓H␈↓␈↓↓u=␈↓¬NIL␈↓↓␈↓␈α
or␈α
␈↓↓n=0␈↓.␈α
 Then␈α
we␈α
figured␈α
out␈α
how␈α
each␈α
partition␈α
could␈α
be␈α
constructed␈α
from␈α
a␈α
partition␈αof␈α
a
␈↓ ↓H␈↓sublist␈α
of␈α
␈↓↓u␈↓␈α
(namely␈α
␈↓↓␈↓αd|␈↓↓u␈↓)␈α
and␈α
put␈α
it␈αall␈α
together␈α
in␈α
a␈α
recursive␈α
definition.␈α
 An␈α
alternate␈αapproach␈α
is
␈↓ ↓H␈↓to␈α⊂define␈α∂a␈α⊂linear␈α⊂ordering␈α∂on␈α⊂the␈α∂set␈α⊂of␈α⊂partitions␈α∂of␈α⊂a␈α∂list␈α⊂␈↓↓u␈↓␈α⊂into␈α∂␈↓↓n␈↓␈α⊂parts␈α∂and␈α⊂then␈α⊂write␈α∂a
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠107


␈↓ ↓H␈↓program␈α
to␈α
enumerate␈α
them␈α
in␈α
order.␈α
 For␈α
example,␈α
we␈α
could␈α
begin␈α
with␈α
the␈α
smallest␈αand␈α
program
␈↓ ↓H␈↓a␈α∂"successor"␈α∂operation␈α∂giving␈α∂the␈α∂next␈α∂partition␈α∂in␈α∂the␈α∂ordering.␈α∂Since␈α∂there␈α∂are␈α∂only␈α∂a␈α∂finite
␈↓ ↓H␈↓number of partitions, the program will eventually terminate with the largest.

␈↓ ↓H␈↓        We␈αorder␈αpartitions␈αas␈αfollows:␈αthe␈αsmallest␈αis␈αobtained␈αby␈αmaking␈αthe␈αfirst␈α␈↓↓n-1␈↓␈αelements␈αof
␈↓ ↓H␈↓the␈α∞partition␈α∞lists␈α
of␈α∞length␈α∞␈↓↓1␈↓␈α∞and␈α
the␈α∞␈↓↓n␈↓th␈α∞element␈α
what␈α∞ever␈α∞is␈α∞left␈α
after␈α∞deleting␈α∞the␈α∞first␈α
␈↓↓n-1␈↓
␈↓ ↓H␈↓members␈αof␈α␈↓↓u.␈↓␈α The␈αlargest␈αpartition␈αis␈αa␈αlist␈αin␈αwhich␈αthe␈αthe␈αfirst␈αelement␈αis␈αthe␈αlist␈αcontaining
␈↓ ↓H␈↓the␈α
first␈α
␈↓↓m␈↓␈αelements␈α
of␈α
␈↓↓u,␈↓␈αwhere␈α
␈↓↓m=length␈α
u-(n-1)␈↓␈αand␈α
the␈α
remaining␈αelements␈α
are␈α
one␈αelement
␈↓ ↓H␈↓lists.␈α In␈αgeneral,␈αthe␈αpartition␈α
␈↓↓w␈↓␈αis␈αgreater␈αthan␈αthe␈αpartition␈α
␈↓↓v␈↓␈αif␈αat␈αthe␈αfirst␈αelement␈α
where␈αthey
␈↓ ↓H␈↓disagree␈αthe␈α
element␈αin␈α
␈↓↓w␈↓␈αis␈α
longer␈αthan␈αthat␈α
of␈α␈↓↓v.␈↓␈α
  The␈αprogram␈α
␈↓↓prtn␈↓␈αgiven␈α
below␈αenumerates
␈↓ ↓H␈↓partitions␈α∂in␈α⊂this␈α∂order.␈α∂ The␈α⊂auxiliary␈α∂␈↓↓maxpart␈↓␈α∂implements␈α⊂the␈α∂definition␈α∂of␈α⊂biggest␈α∂partition
␈↓ ↓H␈↓given␈α⊂above.␈α⊂ The␈α⊂loop␈α∂␈↓¬A␈α⊂␈↓completes␈α⊂an␈α⊂initial␈α∂fragment␈α⊂of␈α⊂a␈α⊂partition␈α∂by␈α⊂adding␈α⊂lists␈α⊂of␈α∂one
␈↓ ↓H␈↓element␈αuntil␈αthe␈α␈↓↓n␈↓th␈αelement␈αis␈αreached.␈α The␈αremainder␈αof␈α␈↓↓u␈↓␈α(stored␈αin␈α␈↓↓v␈↓)␈αis␈αthen␈αadded␈αand␈α
the
␈↓ ↓H␈↓completed␈αpartition␈αis␈αadded␈αto␈α
the␈αcollector␈α␈↓↓pl.␈↓␈α The␈αloop␈α␈↓¬B␈α
␈↓is␈αused␈αto␈αback␈αup␈αthru␈α
a␈αpartition
␈↓ ↓H␈↓until the first position where the length of the entry can be incremented is found.

␈↓ ↓H␈↓↓␈↓ βxprtn[u, n] ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓ [pl, p, v, m]
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓u ∧ n = 0 ␈↓αthen␈↓↓ return <␈↓¬NIL␈↓↓>
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αelse␈↓↓ ␈↓αif␈↓↓ n = 0 ␈↓αthen␈↓↓ return ␈↓¬NIL␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αelse␈↓↓ ␈↓αif␈↓↓ length u < n ␈↓αthen␈↓↓ return ␈↓¬NIL␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧xpl ← ␈↓¬NIL␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧xp ← ␈↓¬NIL␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧xv ← u
␈↓ ↓H␈↓↓␈↓ ∧xm ← n
␈↓ ↓H␈↓↓␈↓ ∧8␈↓¬A:␈↓↓␈↓ ∧x␈↓αif␈↓↓ m = 1 ␈↓αthen␈↓↓ [pl ← reverse v . p . pl, ␈↓αgo to␈↓↓ ␈↓¬B␈↓↓]
␈↓ ↓H␈↓↓1.6)␈↓ ∧xp ← <␈↓αa|␈↓↓v> . p
␈↓ ↓H␈↓↓␈↓ ∧xv ← ␈↓αd|␈↓↓v
␈↓ ↓H␈↓↓␈↓ ∧xm ← sub1 m
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ ␈↓¬A␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8␈↓¬B:␈↓↓␈↓ ∧x␈↓αif␈↓↓ maxpart[␈↓αa|␈↓↓pl, u, n] ␈↓αthen␈↓↓ return pl
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αelse␈↓↓ ␈↓αif␈↓↓ m < length v ␈↓αthen␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧x[p ← [␈↓αa|␈↓↓p * <␈↓αa|␈↓↓v>] . ␈↓αd|␈↓↓p, v ← ␈↓αd|␈↓↓v, ␈↓αgo to␈↓↓ ␈↓¬A␈↓↓]
␈↓ ↓H␈↓↓␈↓ ∧xv ← [␈↓αa|␈↓↓p * v]
␈↓ ↓H␈↓↓␈↓ ∧xp ← ␈↓αd|␈↓↓p
␈↓ ↓H␈↓↓␈↓ ∧xm ← add1 m
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ ␈↓¬B␈↓↓

␈↓ ↓H␈↓↓␈↓ βxmaxpart[w, u, n] ← length u < length ␈↓αa|␈↓↓w + n


␈↓ ↓H␈↓        In␈α∪a␈α∪later␈α∪chapter␈α∪we␈α∪will␈α∪see␈α∪how␈α∪this␈α∪program␈α∪can␈α∪be␈α∪proved␈α∪to␈α∪satisfy␈α∪the␈α∪same
␈↓ ↓H␈↓specification as the recursive version.

␈↓ ↓H␈↓        As␈αpromised,␈αhere␈αare␈α
some␈αcircumstances␈αin␈αwhich␈α
sequential␈αprograms␈αmight␈αbe␈α
preferred
␈↓ ↓H␈↓to functional programs:

␈↓ ↓H␈↓        1.␈αIn␈αsome␈αcases␈αand␈αfor␈αsome␈αpeople,␈αit␈αis␈αeasier␈αto␈αthink␈αabout␈αhow␈αto␈αgo␈αfrom␈αthe␈αinitial
␈↓ ↓H␈↓conditions␈α
step-by-step␈α
to␈α
the␈α
final␈α
result␈α
than␈α
to␈α
think␈α
about␈α
how␈α
the␈α
final␈α
result␈α
can␈αbe␈α
obtained
␈↓ ↓H␈↓from␈αeasier␈αcases.␈α The␈αtwo␈αprograms␈αfor␈αcomputing␈αpartitions␈αare␈αexamples␈αof␈αthese␈αtwo␈αstyles␈αof
␈↓ ↓H␈↓108␈↓ ¬|Chapter  V␈↓ H


␈↓ ↓H␈↓thinking␈α
and␈α
programming.␈α
 There␈α
seems␈αto␈α
be␈α
a␈α
matter␈α
of␈αtaste␈α
to␈α
a␈α
substantial␈α
extent,␈αthough
␈↓ ↓H␈↓the␈α⊂functional␈α∂form␈α⊂seems␈α⊂to␈α∂have␈α⊂clear␈α∂conceptual␈α⊂advantages␈α⊂for␈α∂functions␈α⊂like␈α⊂␈↓↓append␈↓␈α∂and
␈↓ ↓H␈↓␈↓↓subst.␈↓

␈↓ ↓H␈↓        2.␈α⊃When␈α∩we␈α⊃are␈α∩considering␈α⊃a␈α∩program␈α⊃that␈α∩interacts␈α⊃with␈α∩an␈α⊃environment␈α∩instead␈α⊃of
␈↓ ↓H␈↓producing an answer, the sequential form often seems more natural.

␈↓ ↓H␈↓        3.␈αWhen␈αthere␈αare␈αa␈αlarge␈αnumber␈αof␈αvariables,␈αit␈αcan␈αturn␈αout␈αthat␈αthe␈αnecessary␈αfunctions
␈↓ ↓H␈↓have␈α∞an␈α∞unwieldy␈α∞number␈α∂of␈α∞arguments.␈α∞ Frequently␈α∞in␈α∞such␈α∂cases␈α∞only␈α∞a␈α∞few␈α∞of␈α∂the␈α∞variables
␈↓ ↓H␈↓actually change in any "loop" , so it is inefficient to pass them all around.

␈↓ ↓H␈↓        4.␈αA␈αprogram␈αfor␈αcarrying␈αout␈αa␈αtest␈αby␈αsearching␈αa␈αspace␈αof␈αtest␈αcases␈αmay␈αmake␈αa␈αdecision
␈↓ ↓H␈↓at␈αsome␈α
intermediate␈αpoint␈αand␈α
wish␈αto␈α
abort␈αthe␈αsearch␈α
at␈αthis␈α
point.␈α With␈αout␈α
the␈αaid␈αof␈α
special
␈↓ ↓H␈↓control␈α∂mechanisms␈α∂(such␈α∂as␈α∂␈↓↓catch␈↓␈α∂and␈α∂␈↓↓throw␈↓␈α∂to␈α∂be␈α∂discussed␈α∂in␈α∂Chapter␈α∂VI)␈α∂the␈α∂explicit␈α∂loop
␈↓ ↓H␈↓structure of a sequential program is needed to do this efficiently.

␈↓ ↓H␈↓Remarks:

␈↓ ↓H␈↓        1.␈α
Although␈α
␈↓αreturn␈↓␈α
 and␈α
␈↓αgo to␈↓␈α
statements␈α
in␈α
LISP␈α
have␈α
no␈α
meaning␈α
unless␈α
they␈α
occur␈α
inside
␈↓ ↓H␈↓a␈α␈↓αprogram␈↓,␈αan␈αassignment␈α
statement␈αmay␈αbe␈αregarded␈αas␈α
an␈αprogram␈αterm.␈α Thus␈α
␈↓↓[x ← ␈↓αa|␈↓↓y] . z␈↓␈αis
␈↓ ↓H␈↓an abbreviation for ␈↓↓␈↓αprogram␈↓↓[[] x ← ␈↓αa|␈↓↓y; ␈↓αreturn␈↓↓ x] . z␈↓.

␈↓ ↓H␈↓        2.␈α
Besides␈α
␈↓¬SETQ,␈α
␈↓most␈α
LISPs␈α
allow␈α∞assignments␈α
of␈α
the␈α
form␈α
 (␈↓¬SET␈α
␈↓<exp1>␈α∞<exp2>),␈α
where
␈↓ ↓H␈↓<exp1>␈α⊂is␈α⊂evaluated␈α⊂to␈α⊂determine␈α⊂to␈α⊂what␈α⊃variable␈α⊂the␈α⊂assignment␈α⊂is␈α⊂to␈α⊂be␈α⊂made.␈α⊃ If␈α⊂<exp1>
␈↓ ↓H␈↓doesn't evaluate to a variable, an error is signalled.

␈↓ ↓H␈↓        3.␈α⊂ LISPs␈α⊂generally␈α⊂have␈α⊂special␈α⊂form␈α⊂of␈α⊂the␈α⊂␈↓¬PROG␈α⊂␈↓construct␈α⊂for␈α⊂frequently␈α⊃used␈α⊂special
␈↓ ↓H␈↓cases.  For example

␈↓ ↓H␈↓␈↓ ∧h␈↓↓␈↓¬(PROG1 <s1> <s2> . . . <sn>)␈↓↓␈↓ 

␈↓ ↓H␈↓does statements ␈↓¬1 ␈↓to ␈↓¬n ␈↓in order and returns the value returned by ␈↓¬<s1> ␈↓and

␈↓ ↓H␈↓␈↓ ∧h␈↓↓␈↓¬(PROGN <s1> <s2> . . . <sn>)␈↓↓␈↓ 

␈↓ ↓H␈↓does statements ␈↓¬1 ␈↓to ␈↓¬n ␈↓in order and returns the value returned by ␈↓¬sn. ␈↓

␈↓ ↓H␈↓        4.␈α
In␈α
the␈α
conditional␈α
statement␈α
it␈α
is␈α
convenient␈α
to␈α
allow␈α
a␈α
list␈α
of␈α
statements␈α
as␈α
the␈α
␈↓αthen␈↓␈α
clause
␈↓ ↓H␈↓rather␈α
than␈αa␈α
single␈αstatement.␈α
 This␈α
often␈αallows␈α
a␈αmore␈α
natural␈α
organization␈αof␈α
the␈αprogram␈α
and
␈↓ ↓H␈↓causes␈αno␈αdifficulty␈αin␈αthe␈αrewriting␈αof␈αclean␈αsequential␈αprograms␈αas␈αfunctional␈αprograms.␈α In␈αfact
␈↓ ↓H␈↓LISP␈α∞systems␈α∞usually␈α∞allow␈α∂this␈α∞more␈α∞general␈α∞form␈α∞of␈α∂conditional,␈α∞thus␈α∞the␈α∞conditional␈α∂has␈α∞the
␈↓ ↓H␈↓general form

␈↓ ↓H␈↓␈↓ ∧1(␈↓¬COND ␈↓<p1 e11 ... e1k>  ... <pn en1 ... enm>)

␈↓ ↓H␈↓The␈αvalue␈α
returned␈αin␈α
the␈αcase␈α
one␈αis␈α
wanted␈αis␈α
the␈αvalue␈α
of␈αthe␈α
last␈α<eij>␈α
evaluated.␈α This␈α
allows
␈↓ ↓H␈↓the␈αsimulation␈αof␈αa␈αflowchart␈αdescription␈αof␈αa␈αsequential␈αprogram␈αwith␈αout␈αexpliticly␈αlabeling␈αthe
␈↓ ↓H␈↓sequence of instructions that is to follow a particular branch.

␈↓ ↓H␈↓        5.␈α∪In␈α∪writing␈α∪␈↓αprogram␈↓s␈α∪in␈α∪external␈α∪form␈α∩we␈α∪will␈α∪often␈α∪use␈α∪the␈α∪convention␈α∪that␈α∩each
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠109


␈↓ ↓H␈↓statement␈αis␈αwritten␈αon␈αa␈αseparate␈αline.␈α This␈αmeans␈αthat␈αthe␈α";"␈αseparator␈αin␈αnot␈αneeded␈αand␈αmay
␈↓ ↓H␈↓be omitted in this case.



␈↓ ↓H␈↓2.  ␈↓αArrays in LISP␈↓


␈↓ ↓H␈↓        As␈α
mentioned␈α
in␈α∞section␈α
␈↓π∞␈↓1␈α
in␈α∞addition␈α
to␈α
simple␈α
variables␈α∞which␈α
may␈α
be␈α∞assigned␈α
values
␈↓ ↓H␈↓using␈α
the␈αassignment␈α
statement,␈α
LISP␈αadmits␈α
arrays.␈α
 An␈αarray␈α
may␈α
in␈αprinciple␈α
have␈αany␈α
number
␈↓ ↓H␈↓of␈α
dimensions.␈α∞ If␈α
␈↓↓foo␈↓␈α∞is␈α
a␈α∞2-dimensional␈α
(say␈α∞3␈α
x␈α∞4)␈α
array␈α∞then␈α
␈↓↓foo[i,j]␈↓␈α∞can␈α
be␈α∞thought␈α
of␈α∞as␈α
a
␈↓ ↓H␈↓complicated␈α
variable␈α
name.␈α
  It␈α∞evaluates␈α
to␈α
the␈α
jth␈α∞element␈α
of␈α
the␈α
ith␈α
row␈α∞of␈α
␈↓↓foo␈↓␈α
and␈α
it␈α∞can␈α
be
␈↓ ↓H␈↓assigned␈α∃a␈α∃value␈α∀using␈α∃the␈α∃function␈α∃␈↓↓store.␈↓␈α∀ Thus␈α∃␈↓↓store[foo,<2,3>,␈↓¬APPLE␈↓↓]␈↓␈α∃assigns␈α∃to␈α∀row 2
␈↓ ↓H␈↓column 3␈α
of␈α
␈↓↓foo␈↓␈αthe␈α
value␈α
␈↓¬APPLE␈↓.␈α
 Recall␈αthat␈α
in␈α
external␈α
form␈αwe␈α
allowed␈α
subscripted␈αvariables␈α
in
␈↓ ↓H␈↓assignment␈αstatements.␈αThus␈α␈↓↓store[foo,<i,j>,e]␈↓␈αcan␈αalso␈αbe␈αwritten␈α␈↓↓foo[i,j] ← e␈↓.␈α (␈↓↓foo[i,j]␈↓␈α
appearing
␈↓ ↓H␈↓on␈α
the␈α
left␈α
of␈α
an␈α
assignment␈α
denotes␈α
the␈αposition␈α
in␈α
the␈α
array␈α
while␈α
in␈α
other␈α
contexts␈α
it␈αdenotes
␈↓ ↓H␈↓the␈αcontents␈αof␈α
that␈αposition.)␈α It␈α
would␈αnot␈αin␈αprinciple␈α
be␈αdifficult␈αto␈α
modify␈αthe␈α␈↓¬SETQ␈α
␈↓of␈αLISP
␈↓ ↓H␈↓to accept subscripted variables, although there may be practical reasons for not doing so.

␈↓ ↓H␈↓        Before␈α∞using␈α∞an␈α∞array,␈α∞it␈α∞must␈α∞be␈α∞declared␈α∂to␈α∞LISP.␈α∞  ␈↓¬(ARRAY␈α∞FOO␈α∞T␈α∞3␈α∞4)␈↓␈α∞tells␈α∂the␈α∞LISP
␈↓ ↓H␈↓system␈αthat␈αthe␈αatom␈α␈↓¬FOO␈α␈↓is␈αto␈αdenote␈αa␈α3␈αx␈α4␈αarray␈αof␈αS-expressions.␈α (The␈αthird␈αitem␈α␈↓¬T␈↓␈αspecifies
␈↓ ↓H␈↓the␈α∩type␈α∩of␈α∪the␈α∩array.␈α∩There␈α∪are␈α∩other␈α∩possibilities␈α∪for␈α∩type␈α∩such␈α∪as␈α∩␈↓¬FIXNUM␈α∩␈↓or␈α∪␈↓¬FLONUM␈α∩␈↓in
␈↓ ↓H␈↓MACLISP.␈α The␈αform␈αof␈αthe␈αarray␈αdeclaration␈αmay␈αalso␈αvary␈αwith␈αimplementation.)␈αThe␈αinternal
␈↓ ↓H␈↓forms␈α∩for␈α∩the␈α∩above␈α∩expressions␈α∩for␈α∪accessing␈α∩and␈α∩storing␈α∩into␈α∩arrays␈α∩are␈α∩␈↓¬(FOO I J)␈α∪␈↓␈α∩and
␈↓ ↓H␈↓␈↓¬(STORE (FOO 2 3) 'APPLE).␈α⊂␈↓␈α⊂Note␈α⊃that␈α⊂accessing␈α⊂an␈α⊃array␈α⊂element␈α⊂has␈α⊃the␈α⊂same␈α⊂form␈α⊃as␈α⊂a
␈↓ ↓H␈↓function␈αcall.␈α
 LISP␈αknows␈α
that␈α␈↓¬FOO␈α
␈↓is␈αan␈αarray␈α
because␈αyou␈α
declared␈αit␈α
to␈αbe␈α
one␈αusing␈αthe␈α
␈↓¬ARRAY
␈↓ ↓H␈↓¬␈↓command.  This is analogous to telling LISP about a program name using the ␈↓¬DEFUN ␈↓command.

␈↓ ↓H␈↓        In␈α∪addition␈α∩to␈α∪accessing␈α∩and␈α∪storing␈α∪primitives␈α∩for␈α∪operating␈α∩on␈α∪arrays,␈α∪LISP␈α∩usually
␈↓ ↓H␈↓provides␈α∂a␈α∂means␈α∂of␈α⊂convertings␈α∂lists␈α∂to␈α∂arrays␈α∂and␈α⊂vice␈α∂versa.␈α∂ In␈α∂MACLISP␈α∂they␈α⊂are␈α∂called
␈↓ ↓H␈↓␈↓¬FILLARRAY␈α␈↓and␈α␈↓¬LISTARRAY.␈α␈↓␈αThe␈α
following␈αconversation␈αwith␈αMACLISP␈αwill␈αdeclare␈α
a␈αfixnum
␈↓ ↓H␈↓array ␈↓¬TIC ␈↓and fill it with alternating ␈↓¬1␈↓s and ␈↓¬0␈↓s.

␈↓ ↓H␈↓␈↓ αXuser:␈↓ ¬_␈↓¬(ARRAY TIC FIXNUM 3 3)␈↓
␈↓ ↓H␈↓␈↓ αXLISP:␈↓ ¬_␈↓¬TIC␈↓

␈↓ ↓H␈↓␈↓ αXuser:␈↓ ¬_␈↓¬(FILLARRAY 'TIC '(1 0 1 0 1 0 1 0 1))␈↓
␈↓ ↓H␈↓␈↓ αXLISP:␈↓ ¬_␈↓¬TIC ␈↓

␈↓ ↓H␈↓␈↓ αXuser:␈↓ ¬_␈↓¬(LISTARRAY 'TIC)␈↓
␈↓ ↓H␈↓␈↓ αXLISP:␈↓ ¬_␈↓¬(1 0 1 0 1 0 1 0 1)␈↓

␈↓ ↓H␈↓␈↓ αXuser:␈↓ ¬_␈↓¬(TIC 1 0)␈↓
␈↓ ↓H␈↓␈↓ αXLISP:␈↓ ¬_␈↓¬0 ␈↓

␈↓ ↓H␈↓␈↓ αXuser:␈↓ ¬_␈↓¬(TIC 1 1)␈↓
␈↓ ↓H␈↓␈↓ αXLISP:␈↓ ¬_␈↓¬1 ␈↓

␈↓ ↓H␈↓Notice␈α
that␈α∞(i)␈α
the␈α
value␈α∞of␈α
␈↓¬FILLARRAY␈α
␈↓is␈α∞the␈α
name␈α∞of␈α
the␈α
array␈α∞and␈α
not␈α
the␈α∞array␈α
and␈α∞(ii)␈α
the
␈↓ ↓H␈↓indices␈α
start␈α
at␈α0.␈α
 Note␈α
also␈αthat␈α
general␈α
arrays␈α(of␈α
type␈α
␈↓¬T␈↓)␈αcan␈α
arbitrary␈α
entries␈α
including␈αarray
␈↓ ↓H␈↓names and arrays pointers.
␈↓ ↓H␈↓110␈↓ ¬|Chapter  V␈↓ H


␈↓ ↓H␈↓Arrays␈αare␈αare␈αpractical␈αfor␈αtwo␈αreasons.␈α One␈αis␈αquick␈αaccess␈αto␈αindividual␈αelements,␈αthe␈αother␈αis
␈↓ ↓H␈↓that␈αthey␈αare␈αupdated␈αrather␈αthan␈αcopied␈αwhich␈αsaves␈αspace.␈α  One␈αcould␈αcompare␈αthe␈αuse␈αof␈αlists
␈↓ ↓H␈↓vs␈α∞arrays␈α
for␈α∞storing␈α∞data␈α
to␈α∞the␈α∞use␈α
of␈α∞tapes␈α∞vs␈α
disks.␈α∞  Lists␈α∞are␈α
somewhat␈α∞more␈α∞flexible␈α
than
␈↓ ↓H␈↓tape␈α
in␈α∞that␈α
you␈α
can␈α∞splice␈α
and␈α∞chop␈α
somewhat␈α
more␈α∞readily␈α
but␈α
the␈α∞sequential␈α
access␈α∞vs␈α
direct
␈↓ ↓H␈↓access analogy is good.

␈↓ ↓H␈↓In␈α
LISP␈α
arrays␈αmight␈α
be␈α
used␈α
to␈αrepresent␈α
game␈α
boards␈α
in␈αgame␈α
playing␈α
programs,␈α
or␈αto␈α
represent
␈↓ ↓H␈↓a␈αpicture␈αas␈αa␈α"pixel"␈αarray.␈α We␈αwill␈αsee␈α
an␈αexmple␈αof␈αthe␈αformer␈αin␈αChapter␈α.␈α As␈αto␈α
the␈αlatter,
␈↓ ↓H␈↓the␈αprogram␈α
␈↓↓picture find␈↓␈αscans␈α
a␈α␈↓↓picture␈↓␈α
represented␈αas␈α
an␈αarray␈α
(say␈αof␈α
colors)␈αfor␈α
all␈αand␈α
returns
␈↓ ↓H␈↓a␈α∞list␈α∞of␈α∞all␈α∞locations␈α∞where␈α∞a␈α∞given␈α∞picture␈α∞specification␈α∞␈↓↓pat␈↓␈α∞is␈α∞satisfied.␈α∞ The␈α∞program␈α
␈↓↓pmatch␈↓
␈↓ ↓H␈↓checks␈αto␈αsee␈αif␈αthe␈αspecification␈α␈↓↓pat␈↓␈αis␈αsatisfied␈αat␈α␈↓↓pic[i,j].␈↓␈α A␈αspecification␈αis␈αa␈αlist␈αof␈αtriples␈α␈↓↓<rd
␈↓ ↓H␈↓↓cd␈α∞pred>␈↓.␈α∞ It␈α∞is␈α∞satisfied␈α∞at␈α∞␈↓↓pic[i,j]␈↓␈α∞if␈α∞each␈α∞of␈α∞the␈α∞triples␈α∞is.␈α∞ A␈α∞triple␈α∞is␈α∞satisfied␈α∞if␈α∞the␈α∞location
␈↓ ↓H␈↓␈↓↓<i+rd,j+cd>␈↓ is within the picture and ␈↓↓pred[pic[i+rd,j+cd]]␈↓ holds.

␈↓ ↓H␈↓↓␈↓ β(pmatch[pat, pic, i, j] ←
␈↓ ↓H␈↓↓␈↓ βh␈↓αprogram␈↓↓ [s, p, r, c, m, n]
␈↓ ↓H␈↓↓␈↓ ∧(m ← sub1 ␈↓αad|␈↓↓arraydims pic
␈↓ ↓H␈↓↓␈↓ ∧(n ← sub1 ␈↓αadd|␈↓↓arraydims pic
␈↓ ↓H␈↓↓␈↓ ∧(p ← pat
␈↓ ↓H␈↓↓␈↓ βh␈↓¬LOOP:␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧(␈↓αif␈↓↓ ␈↓αn|␈↓↓p ␈↓αthen␈↓↓ return ␈↓¬T␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧(s ← ␈↓αa|␈↓↓p
␈↓ ↓H␈↓↓␈↓ ∧(␈↓αa|␈↓↓s
␈↓ ↓H␈↓↓␈↓ ∧(␈↓αif␈↓↓ r < 0 ∨ m < r ␈↓αthen␈↓↓ return ␈↓¬NIL␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧(c ← j + ␈↓αad|␈↓↓s
␈↓ ↓H␈↓↓␈↓ ∧(␈↓αif␈↓↓ c < 0 ∨ n < c ␈↓αthen␈↓↓ return ␈↓¬NIL␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧(␈↓αif␈↓↓ apply[␈↓αadd|␈↓↓s, <apply[pic, <r, c>]>] ␈↓αthen␈↓↓ [p ← ␈↓αd|␈↓↓p, ␈↓αgo to␈↓↓ ␈↓¬LOOP␈↓↓]
␈↓ ↓H␈↓↓␈↓ ∧(return ␈↓¬NIL␈↓↓

␈↓ ↓H␈↓↓2.1)

␈↓ ↓H␈↓↓␈↓ β(picture find[pic, pat] ←
␈↓ ↓H␈↓↓␈↓ βhprog [i, j, m, n, locs]
␈↓ ↓H␈↓↓␈↓ ∧(m ← sub1 ␈↓αad|␈↓↓arraydims pic
␈↓ ↓H␈↓↓␈↓ ∧(n ← sub1 ␈↓αadd|␈↓↓arraydims pic
␈↓ ↓H␈↓↓␈↓ ∧(i ← 0
␈↓ ↓H␈↓↓␈↓ ∧(j ← 0
␈↓ ↓H␈↓↓␈↓ ∧(locs ← ␈↓¬NIL␈↓↓
␈↓ ↓H␈↓↓␈↓ βh␈↓¬LOOP:␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧(␈↓αif␈↓↓ pmatch[pat, pic, i, j] ␈↓αthen␈↓↓ locs ← [i . j] . locs
␈↓ ↓H␈↓↓␈↓ ∧(␈↓αif␈↓↓ j < n ␈↓αthen␈↓↓ [j ← add1 j, ␈↓αgo to␈↓↓ ␈↓¬LOOP␈↓↓]
␈↓ ↓H␈↓↓␈↓ ∧(␈↓αif␈↓↓ i < m ␈↓αthen␈↓↓ [i ← add1 i, j ← 0, ␈↓αgo to␈↓↓ ␈↓¬LOOP␈↓↓]
␈↓ ↓H␈↓↓␈↓ ∧(return locs


␈↓ ↓H␈↓For example if we define the array ␈↓¬PIX ␈↓as follows
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠111



␈↓ ↓H␈↓¬␈↓ β8(ARRAY PIX T 6 6)

␈↓ ↓H␈↓¬␈↓ β8(FILLARRAY 'PIX
␈↓ ↓H␈↓¬␈↓ β8        (W R W G W B)
␈↓ ↓H␈↓¬␈↓ β8        (R W G W B W)
␈↓ ↓H␈↓¬␈↓ β8        (W G W B W R)
␈↓ ↓H␈↓¬␈↓ β8        (G W B W R W)
␈↓ ↓H␈↓¬␈↓ β8        (W B W R W G)
␈↓ ↓H␈↓¬␈↓ β8        (B W R W G W) )


␈↓ ↓H␈↓and the specification ␈↓¬PAT ␈↓as follows

␈↓ ↓H␈↓¬␈↓ β8(SETQ S1 '(0 0 (FUNCTION (LAMBDA (X) (EQ X 'W))) ) )
␈↓ ↓H␈↓¬␈↓ β8(SETQ S2 '(0 1 (FUNCTION (LAMBDA (X) (EQ X 'R))) ) )
␈↓ ↓H␈↓¬␈↓ β8(SETQ S3 '(1 0 (FUNCTION (LAMBDA (X) (EQ X 'R))) ) )
␈↓ ↓H␈↓¬␈↓ β8(SETQ S4 '(-1 0 (FUNCTION (LAMBDA (X) (EQ X 'B))) ) )
␈↓ ↓H␈↓¬␈↓ β8(SETQ S5 '(0 -1 (FUNCTION (LAMBDA (X) (EQ X 'B))) ) )

␈↓ ↓H␈↓¬␈↓ β8(SETQ PAT (LIST S1 S2 S3 S4 S5))


␈↓ ↓H␈↓then the list of locations satifying the specification is obtained by

␈↓ ↓H␈↓¬␈↓ β8(PATTERN FIND 'PIX PAT)

␈↓ ↓H␈↓¬␈↓ β8((4 . 2) (3 . 3) (2 . 4))


␈↓ ↓H␈↓        We␈α∃can␈α∃begin␈α∀to␈α∃characterize␈α∃arrays␈α∀in␈α∃LISP␈α∃by␈α∀modifying␈α∃ the␈α∃characterization␈α∀of
␈↓ ↓H␈↓assignment␈α
and␈αcontents␈α
functions␈α
on␈α state␈α
vectors␈αgiven␈α
in␈α
McCarthy␈α[1962b].␈α
  Thus␈α
we␈αhave
␈↓ ↓H␈↓functions␈α≠␈↓↓a[array,pos,exp]␈↓␈α≠and␈α≠␈↓↓c[array,pos]␈↓␈α≠where␈α≠␈↓↓a␈↓␈α≠returns␈α≠the␈α≠array␈α≠resulting␈α~from
␈↓ ↓H␈↓␈↓↓store[array,pos,exp]␈↓␈α
(in␈α
LISP␈α
␈↓↓store␈↓␈α
returns␈α
the␈α
value␈α
of␈α
␈↓↓exp␈↓)␈α
and␈α
␈↓↓c␈↓␈α
returns␈α
the␈α
contents␈α
of␈α␈↓↓array␈↓␈α
at
␈↓ ↓H␈↓␈↓↓pos␈↓ (like writing ␈↓↓array[pos]␈↓).  The functions ␈↓↓a␈↓ and ␈↓↓c␈↓ satisfy  the following relations

␈↓ ↓H␈↓2.2)␈↓ α8␈↓↓c[a[array,pos,exp],pos1] = ␈↓αif␈↓↓ pos=pos1 ␈↓αthen␈↓↓ exp ␈↓αelse␈↓↓ c[array,pos1]␈↓

␈↓ ↓H␈↓2.3)␈↓ α8␈↓↓array = a[array,pos,c[array,pos]]␈↓

␈↓ ↓H␈↓2.4)␈↓ α8␈↓↓a[a[array,pos0,exp0],pos1,exp1] =␈↓ ε8␈↓αif␈↓↓ pos0=pos1 ␈↓αthen␈↓↓ a[array,pos0,exp1]␈↓
␈↓ ↓H␈↓␈↓ ε8␈↓↓␈↓αelse␈↓↓ a[a[array,pos1,exp1],pos0,exp0]␈↓

␈↓ ↓H␈↓These␈αequations␈αcharacterize␈αthe␈α
abstract␈αnotion␈αof␈αarray.␈α We␈α
also␈αneed␈αto␈αextend␈αthe␈α
notion␈αof
␈↓ ↓H␈↓equality␈αto␈αarrays␈αin␈αthe␈αobvious␈αway.␈α (Namely␈αtwo␈αarrays␈αare␈αequal␈αif␈αand␈αonly␈αif␈αthe␈αthey␈αhave
␈↓ ↓H␈↓the␈α
same␈α
set␈α
of␈α∞allowed␈α
positions␈α
and␈α
the␈α∞contents␈α
of␈α
any␈α
allowed␈α∞position␈α
is␈α
the␈α
same␈α∞for␈α
both
␈↓ ↓H␈↓arrays.)␈αThey␈α
do␈αnot␈αdeal␈α
with␈αthe␈αfact␈α
that␈αthe␈α␈↓↓store␈↓␈α
function␈αof␈αLISP␈α
destroys␈αthe␈αold␈α
array␈αin
␈↓ ↓H␈↓the␈αprocess␈αof␈αcreating␈αthe␈αnew␈αversion.␈α They␈αcan␈αbe␈αused␈αto␈αprove␈αpropertied␈αof␈αprograms␈αthat
␈↓ ↓H␈↓have array type arguments.

␈↓ ↓H␈↓For␈αadditional␈αarray␈αfunctions␈α
and␈αprecise␈αspecification␈αof␈α
features␈αthe␈αreader␈αshould␈α
consult␈αthe
␈↓ ↓H␈↓programmers manual for the version of LISP being used.
␈↓ ↓H␈↓112␈↓ ¬|Chapter  V␈↓ H


␈↓ ↓H␈↓3.  ␈↓αDefining macros in LISP␈↓


␈↓ ↓H␈↓        A␈α∞macro␈α∂is␈α∞a␈α∂form␈α∞of␈α∞shorthand␈α∂or␈α∞"syntatic␈α∂sugar".␈α∞ A␈α∞macro␈α∂definition␈α∞tells␈α∂LISP␈α∞that
␈↓ ↓H␈↓when␈α∞it␈α
sees␈α∞the␈α∞defined␈α
form␈α∞it␈α
should␈α∞first␈α∞"decode"␈α
it␈α∞obtaining␈α
the␈α∞intended␈α∞expression␈α
and
␈↓ ↓H␈↓then␈αevaluate␈αthe␈αresult.␈α For␈αexample,␈αwe␈αmight␈αwish␈αto␈αhave␈αa␈αgenuine␈α␈↓αif-then-else␈↓␈αconditional.
␈↓ ↓H␈↓Thus we would write

␈↓ ↓H␈↓␈↓ ∧ ␈↓↓␈↓¬(IF (ATOM X) (LIST X) 'NOT-AN-ATOM-X)␈↓↓␈↓ 

␈↓ ↓H␈↓and the expression evaluated would be

␈↓ ↓H␈↓␈↓ βX␈↓↓␈↓¬(COND ((ATOM X) (LIST X)) (T 'NOT-AN-ATOM-X)).␈↓↓␈↓ 

␈↓ ↓H␈↓The␈αreason␈αfor␈αusing␈αmacros␈αis␈αgenerally␈αto␈αwrite␈αprograms␈αthat␈αare␈αmore␈αcompact␈αand␈αsuggestive
␈↓ ↓H␈↓of their intent.  For example defining

␈↓ ↓H␈↓␈↓ ¬x␈↓↓␈↓¬(BODY <e>)␈↓↓␈↓ 

␈↓ ↓H␈↓which returns the body of a λ-expression to be

␈↓ ↓H␈↓␈↓ ¬p␈↓↓␈↓¬(CADDR <e>)␈↓↓␈↓ 

␈↓ ↓H␈↓and␈αother␈α
such␈αmnemonic␈α
devices␈αmight␈α
make␈α␈↓↓eval␈↓␈α
more␈αreadable.␈α
 You␈αmight␈α
ask␈αwhy␈α
not␈αjust
␈↓ ↓H␈↓define␈αthe␈α
appropriate␈αfunctions?␈α
  There␈αare␈αa␈α
couple␈αof␈α
reasons.␈α One␈αis␈α
that␈αcompilers␈α
can␈αbe
␈↓ ↓H␈↓made␈α
to␈α
expand␈α
simple␈α
macros␈α
before␈α
compiling␈αthe␈α
code,␈α
the␈α
eliminating␈α
one␈α
or␈α
more␈α
levels␈αof
␈↓ ↓H␈↓function␈αcall␈αfor␈αeach␈αinstance.␈α Also␈αin␈αthe␈αcase␈αof␈α␈↓¬IF␈α␈↓a␈αfunction␈αdefinition␈α(of␈αthe␈αordinary␈αsort)
␈↓ ↓H␈↓would␈αnot␈αwork␈αas␈αonly␈αone␈αof␈αthe␈αlatter␈αtwo␈αarguments␈αto␈α␈↓¬IF␈α␈↓␈αshould␈αbe␈αevaluated␈αnot␈αboth.␈α  It
␈↓ ↓H␈↓is␈αalso␈αthe␈αcase␈αthat␈αthe␈αtreatment␈αof␈αmacros␈αreflects␈αthe␈αintent␈αof␈αsuch␈αdefinitions␈αmore␈αaccurately
␈↓ ↓H␈↓that function definitions.

␈↓ ↓H␈↓        The basic macro facility of LISP is very simple.  A definition has the form

␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓¬(DEFUN <macname> MACRO (<var>) <macbody>).␈↓↓␈↓ 

␈↓ ↓H␈↓This␈αhas␈αthe␈αeffect␈αof␈α
associating␈αwith␈α␈↓¬<macname>␈α␈↓a␈α␈↓¬MACRO␈α
␈↓property␈αwhich␈αis␈αa␈αλ-expression␈α
with
␈↓ ↓H␈↓a␈α∩single␈α∩variable␈α⊃␈↓¬<var>␈α∩␈↓and␈α∩body␈α∩␈↓¬<macbody>.␈α⊃␈↓␈α∩When␈α∩LISP␈α⊃encounters␈α∩a␈α∩term␈α∩whose␈α⊃first
␈↓ ↓H␈↓element␈α∞is␈α
an␈α∞atom␈α∞with␈α
a␈α∞␈↓¬MACRO␈α∞␈↓property␈α
then␈α∞␈↓¬<macbody>␈α
␈↓is␈α∞evaluated␈α∞with␈α
␈↓¬<var>␈α∞␈↓␈α∞bound␈α
to
␈↓ ↓H␈↓the␈α⊂entire␈α⊂term.␈α⊃ This␈α⊂produces␈α⊂the␈α⊃expression␈α⊂whose␈α⊂value␈α⊂is␈α⊃desired␈α⊂and␈α⊂that␈α⊃expression␈α⊂is
␈↓ ↓H␈↓evaluated to determine the value of the term.  For example the ␈↓¬BODY ␈↓macro is defined by

␈↓ ↓H␈↓␈↓ βX␈↓↓␈↓¬(DEFUN BODY MACRO (L) (LIST 'CADDR (CADR L)) )␈↓↓␈↓ 

␈↓ ↓H␈↓and the ␈↓¬IF ␈↓macro by

␈↓ ↓H␈↓␈↓ ↓P␈↓↓␈↓¬(DEFUN IF MACRO (L) (LIST 'COND (LIST (CADR L) (CADDR L)) (LIST T (CADDDR L))))␈↓↓␈↓ 

␈↓ ↓H␈↓Then when LISP encounters

␈↓ ↓H␈↓␈↓ ¬_␈↓↓␈↓¬(BODY '(LAMBDA (X) X))␈↓↓␈↓ 
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠113


␈↓ ↓H␈↓it will macro expand this to

␈↓ ↓H␈↓␈↓ ¬_␈↓↓␈↓¬(CADDR '(LAMBDA (X) X)␈↓↓␈↓ 

␈↓ ↓H␈↓which then evaluates to ␈↓¬X. ␈↓ When it encounters

␈↓ ↓H␈↓␈↓ β_␈↓↓␈↓¬((LAMBDA (X) (IF (ATOM X) (LIST X) 'NOT-AN-ATOM)) 'AB)␈↓↓␈↓ 

␈↓ ↓H␈↓It will evaluate

␈↓ ↓H␈↓␈↓ βp␈↓↓␈↓¬(COND ((ATOM X) (LIST X)) (T 'NOT-AN-ATOM))␈↓↓␈↓ 

␈↓ ↓H␈↓in␈α
a␈α
environment␈α
with␈α
␈↓¬X␈α
␈↓bound␈α
to␈α
AB␈α
and␈α
return␈α
␈↓¬(AB).␈α
 As␈α
␈↓a␈α
slightly␈α
more␈α∞complex␈α
example
␈↓ ↓H␈↓consider␈α∃extending␈α∃␈↓↓cons␈↓␈α∀to␈α∃an␈α∃arbitrary␈α∀number␈α∃of␈α∃arguments␈α∀by␈α∃right␈α∃associating.␈α∀ Thus
␈↓ ↓H␈↓␈↓↓rcons[x1,x2,x3]␈↓␈α∩stands␈α∩for␈α∩␈↓↓cons[x1,cons[x2,x3]].␈↓␈α∩ This␈α∩can␈α∩be␈α∩done␈α∩by␈α∩the␈α∩following␈α∩macro
␈↓ ↓H␈↓definition:

␈↓ ↓H␈↓¬␈↓ βλ(DEFUN RCONS MACRO (L)
␈↓ ↓H␈↓¬␈↓ βλ  (COND ((NULL (CDDR L)) (CADR L))
␈↓ ↓H␈↓¬␈↓ βλ        (T (LIST 'CONS (CADR L) (CONS 'RCONS (CDDR L)))) ))


␈↓ ↓H␈↓Several␈α∂of␈α∂the␈α∞built␈α∂in␈α∂LISP␈α∂programs␈α∞that␈α∂take␈α∂an␈α∂arbitrary␈α∞number␈α∂of␈α∂arguments␈α∂are␈α∞infact
␈↓ ↓H␈↓implemented␈α∩as␈α∩macros␈α⊃based␈α∩on␈α∩a␈α∩program␈α⊃taking␈α∩2␈α∩arguments.␈α⊃ ␈↓¬PLUS␈α∩␈↓and␈α∩␈↓¬TIMES␈α∩␈↓are␈α⊃two
␈↓ ↓H␈↓expamples.

␈↓ ↓H␈↓        Although␈α
the␈α
macro␈α
facility␈α∞is␈α
very␈α
powerful␈α
(we␈α
have␈α∞not␈α
even␈α
touched␈α
the␈α∞surface␈α
with
␈↓ ↓H␈↓these␈α∂examples)␈α∂it␈α∂is␈α∂rather␈α∂clumsy␈α∂to␈α∂use␈α∂for␈α∂the␈α∂simple␈α∂kinds␈α∂of␈α∂macros␈α∂most␈α∂often␈α∂used.␈α∞ A
␈↓ ↓H␈↓simple but useful form of macro definition is the following:

␈↓ ↓H␈↓␈↓ ∧`␈↓↓␈↓¬(MACDEF <macform> <macbody>).␈↓↓␈↓ 

␈↓ ↓H␈↓Where␈α∞␈↓¬<macform>␈α
␈↓is␈α∞a␈α
list␈α∞with␈α
the␈α∞macro␈α
name␈α∞as␈α
the␈α∞first␈α
element␈α∞and␈α∞parameters␈α
following.
␈↓ ↓H␈↓When␈α∞a␈α∞macro␈α∞defined␈α∞in␈α
this␈α∞way␈α∞is␈α∞encountered␈α∞the␈α
actual␈α∞arguments␈α∞are␈α∞substituted␈α∞for␈α
the
␈↓ ↓H␈↓parameters␈α∞in␈α∞␈↓¬<macbody>␈α∞␈↓and␈α
the␈α∞resulting␈α∞expression␈α∞evaluated␈α
for␈α∞the␈α∞value.␈α∞ The␈α∞␈↓¬BODY␈α
␈↓and
␈↓ ↓H␈↓␈↓¬IF ␈↓macros can be defined this way as follows.

␈↓ ↓H␈↓␈↓ ∧_␈↓↓␈↓¬(MACDEF (IF A B C) (COND (A B) (T C)))␈↓↓␈↓ 

␈↓ ↓H␈↓␈↓ ∧p␈↓↓␈↓¬(MACDEF (BODY E) (CADDR E))␈↓↓␈↓ 

␈↓ ↓H␈↓Much␈α⊂easier!␈α∂ The␈α⊂␈↓¬MACDEF␈α∂␈↓feature␈α⊂can␈α∂be␈α⊂implemented␈α∂as␈α⊂a␈α∂macro-macro␈α⊂which␈α⊂converts␈α∂the
␈↓ ↓H␈↓higher␈α
level␈α
definition␈α
into␈αa␈α
low␈α
level␈α
definition␈α(now␈α
we␈α
are␈α
beginning␈αto␈α
see␈α
some␈α
of␈αthe␈α
power)
␈↓ ↓H␈↓as follows.
␈↓ ↓H␈↓114␈↓ ¬|Chapter  V␈↓ H



␈↓ ↓H␈↓¬␈↓ αh(DEFUN MACDEF MACRO (L)
␈↓ ↓H␈↓¬␈↓ αh  (LIST 'DEFUN
␈↓ ↓H␈↓¬␈↓ αh        (CAADR L)
␈↓ ↓H␈↓¬␈↓ αh        'MACRO
␈↓ ↓H␈↓¬␈↓ αh        '(%L)
␈↓ ↓H␈↓¬␈↓ αh        (LIST 'SUBLIS
␈↓ ↓H␈↓¬␈↓ αh              (LIST 'PRUP (LIST 'QUOTE (CDADR L)) (LIST 'CDR '%L) )
␈↓ ↓H␈↓¬␈↓ αh              (LIST 'QUOTE (CADDR L))) ))


␈↓ ↓H␈↓where ␈↓¬PRUP ␈↓is the program for converting a pair of lists into an a-list given in Chapter I ␈↓π∞␈↓13.

␈↓ ↓H␈↓        A somewhat more sophisticated macro-macro has the form

␈↓ ↓H␈↓␈↓ ∧H␈↓↓␈↓¬(PMACDEF <macpattern> <macbody>)␈↓↓␈↓ 

␈↓ ↓H␈↓Where␈α↔␈↓¬<macpattern>␈α↔␈↓has␈α↔the␈α↔form␈α_␈↓¬(<macname>␈α↔<p1>␈α↔. . .␈α↔<pn>)␈↓␈α↔and␈α↔the␈α_␈↓¬<pi>␈α↔␈↓are
␈↓ ↓H␈↓elementary␈α∪patterns␈α∪which␈α∪are␈α∀either␈α∪atoms␈α∪whose␈α∪pname␈α∀begins␈α∪with␈α∪␈↓¬?,␈α∪␈↓which␈α∀match␈α∪an
␈↓ ↓H␈↓arbitrary␈αatom,␈αatoms␈αwhose␈α
pname␈αbegins␈αwith␈α␈↓¬*␈α␈↓which␈α
match␈αlist␈αsegments␈αor␈αany␈α
other␈αatoms
␈↓ ↓H␈↓which␈α∀mathch␈α∀only␈α∀themselves.␈α∀ When␈α∀a␈α∃term␈α∀having␈α∀a␈α∀macro␈α∀definition␈α∀of␈α∀this␈α∃form␈α∀is
␈↓ ↓H␈↓encountered,␈α∃the␈α∀argument␈α∃list␈α∀is␈α∃matched␈α∃to␈α∀the␈α∃pattern␈α∀sequence.␈α∃  Elements␈α∃matching␈α∀␈↓¬?
␈↓ ↓H␈↓¬␈↓variables␈α∃are␈α∃substituted␈α∃for␈α∃the␈α∀corresponding␈α∃variable␈α∃in␈α∃␈↓¬<macbody>␈α∃␈↓while␈α∃list␈α∀segments
␈↓ ↓H␈↓matching␈α_␈↓¬*␈α→␈↓variables␈α_are␈α→spliced␈α_into␈α_lists␈α→where␈α_the␈α→corresponding␈α_variable␈α→occurs␈α_in
␈↓ ↓H␈↓␈↓¬<macbody>. ␈↓ For example

␈↓ ↓H␈↓␈↓ α_␈↓↓␈↓¬(PMACDEF (LET *vars BE *vals IN ?body) ((LAMBDA (*vars) ?body) *vals))␈↓↓␈↓ 

␈↓ ↓H␈↓implements the usual ␈↓¬LET ␈↓construct for variable scoping.

␈↓ ↓H␈↓The␈α␈↓¬PMACDEF␈α␈↓facility␈α
can␈αbe␈αimplemented␈αas␈α
a␈αmacro-macro␈αwritten␈αin␈α
terms␈αof␈αa␈αsuitable␈α
pattern
␈↓ ↓H␈↓matcher␈αand␈αpattern␈αinstantiater.␈α Clearly␈αthe␈αmore␈αsophisticated␈αthe␈αpattern␈αmatcher␈αis,␈αthe␈αmore
␈↓ ↓H␈↓sophisticated␈α∂this␈α∂macro␈α∂definition␈α∂form␈α⊂will␈α∂be.␈α∂ We␈α∂will␈α∂have␈α⊂more␈α∂to␈α∂say␈α∂about␈α∂this␈α⊂in␈α∂the
␈↓ ↓H␈↓chapter on pattern matching.

␈↓ ↓H␈↓[Remark: ␈↓¬MACDEF ␈↓and ␈↓¬PMACDEF ␈↓are simplified versions of macro-macros available in MACLISP.]

␈↓ ↓H␈↓As␈α
we␈αsee,␈α
macros␈αprovide␈α
the␈αprogrammer␈α
with␈αthe␈α
ability␈αto␈α
implement␈αboth␈α
data␈αstructures␈α
and
␈↓ ↓H␈↓high␈α
level␈α
program␈α
constructs.␈α
 One␈α
possible␈α
point␈αof␈α
view␈α
in␈α
language␈α
design␈α
is␈α
to␈α
provide␈αa␈α
very
␈↓ ↓H␈↓simple␈α∞basic␈α∞language␈α∂(such␈α∞as␈α∞pure␈α∂LISP)␈α∞and␈α∞a␈α∞powerful␈α∂macro␈α∞definition␈α∞facility␈α∂interms␈α∞of
␈↓ ↓H␈↓which␈αa␈αhigher␈αlevel␈αlanguages␈αcan␈αbe␈αimplemented.␈α With␈αa␈αgood␈αcompiler␈αthis␈αneed␈αnot␈αreduce
␈↓ ↓H␈↓the␈α∂efficiency␈α∂of␈α⊂compiled␈α∂programs␈α∂and␈α∂provides␈α⊂the␈α∂programmer␈α∂with␈α∂great␈α⊂flexibility.␈α∂ The
␈↓ ↓H␈↓RABBIT compiler for SCHEME [Steele 1978] implements this point of view.

␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓1.  What is the value of ␈↓¬(RCONS 'A 'B 'C)␈↓?

␈↓ ↓H␈↓2.␈α∀ Write␈α∀a␈α∪definition␈α∀for␈α∀the␈α∀macro␈α∪␈↓¬LCONS␈α∀␈↓that␈α∀applies␈α∀␈↓¬CONS␈α∪␈↓to␈α∀an␈α∀arbitrary␈α∀number␈α∪of
␈↓ ↓H␈↓arguments␈α;using␈α<left␈α;association␈α;rather␈α<than␈α;right␈α<association.␈α; Thus
␈↓ ↓H␈↓␈↓↓lcons[x1,x2,x3]=cons[cons[x1,x2],x3].␈↓
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠115


␈↓ ↓H␈↓4.  ␈↓αPseudo-functions that modify list structures.␈↓


␈↓ ↓H␈↓        In␈α∂pure␈α∂LISP,␈α⊂list␈α∂structure␈α∂is␈α⊂never␈α∂changed.␈α∂ ␈↓↓car␈↓␈α⊂and␈α∂␈↓↓cdr␈↓␈α∂merely␈α⊂move␈α∂about␈α∂in␈α⊂a␈α∂list
␈↓ ↓H␈↓structure,␈αand␈α␈↓↓cons␈↓␈αcreates␈αnew␈αlist␈αstructure␈α
from␈αthe␈αfree␈αstorage␈αlist.␈α Of␈αcourse,␈αa␈α
variable␈αcan
␈↓ ↓H␈↓get␈αa␈αnew␈α
value␈αthat␈αcorresponds,␈αfor␈α
instance,␈αto␈αputting␈αan␈α
element␈αon␈αthe␈αend␈α
of␈αa␈αlist,␈α
but␈αin
␈↓ ↓H␈↓pure␈α∞LISP␈α
this␈α∞can␈α
only␈α∞be␈α
accomplished␈α∞by␈α∞creating␈α
new␈α∞list␈α
structure.␈α∞ In␈α
this␈α∞section␈α∞we␈α
will
␈↓ ↓H␈↓discuss␈αoperations␈αthat␈αactually␈αmodify␈αlist␈αstructure.␈α They␈αoften␈αhave␈αefficiency␈α
advantages,␈αbut
␈↓ ↓H␈↓their␈α∩use␈α∩interferes␈α∩with␈α∩saving␈α∩memory␈α⊃by␈α∩using␈α∩merging␈α∩list␈α∩structure,␈α∩and␈α∩techniques␈α⊃for
␈↓ ↓H␈↓proving correctness of such programs are not well developed.

␈↓ ↓H␈↓        The␈α∀simplest␈α∀way␈α∀to␈α∀express␈α∀operations␈α∀that␈α∀modify␈α∀list␈α∀structure␈α∀is␈α∀in␈α∀the␈α∀form␈α∪of
␈↓ ↓H␈↓assignments␈α∀to␈α∀␈↓↓car-cdr␈↓␈α∀chains␈α∀of␈α∀variables,␈α∀e.g.␈α∀we␈α∀may␈α∀write␈α∀␈↓↓␈↓αada|␈↓↓x ← y . z␈↓.␈α∀ The␈α∀effect␈α∀of
␈↓ ↓H␈↓executing␈α
this␈α
statement␈α
is␈α
to␈α
replace␈α
the␈α
␈↓αada|␈↓␈α
part␈α
of␈α
the␈α
list␈α
structure␈α
pointed␈α
to␈α
by␈α
␈↓↓x␈↓␈α∞with␈α
the
␈↓ ↓H␈↓value␈α⊂of␈α⊂the␈α⊃right␈α⊂hand␈α⊂side.␈α⊃ As␈α⊂a␈α⊂term,␈α⊂the␈α⊃statement␈α⊂takes␈α⊂the␈α⊃updated␈α⊂value␈α⊂of␈α⊃the␈α⊂first
␈↓ ↓H␈↓argument.␈α∞In␈α∞internal␈α∞notation,␈α∂we␈α∞use␈α∞the␈α∞pseudo-functions␈α∞␈↓¬(RPLACA X Y)␈↓␈α∂and␈α∞␈↓¬(RPLACD X Y)␈↓
␈↓ ↓H␈↓which correspond to the statements ␈↓↓␈↓αa|␈↓↓x ← y␈↓ and ␈↓↓␈↓αd|␈↓↓x ← y␈↓ respectively.

␈↓ ↓H␈↓        ␈↓¬RPLACA␈α␈↓and␈α␈↓¬RPLACD␈α
␈↓are␈αhighly␈αunclean.␈α The␈α
effect␈αof␈αevaluating␈αan␈α
expression␈αinvolving
␈↓ ↓H␈↓␈↓¬RPLACA␈α∞␈↓or␈α∞␈↓¬RPLACD␈α∂␈↓␈α∞depends␈α∞on␈α∂the␈α∞state␈α∞of␈α∂list␈α∞structure␈α∞and␈α∂not␈α∞merely␈α∞on␈α∂the␈α∞S-expressions
␈↓ ↓H␈↓that the variables have as values.  Consider the programs ␈↓↓test1␈↓ and ␈↓↓test2␈↓ given by

␈↓ ↓H␈↓↓␈↓ βxtest1[] ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[]
␈↓ ↓H␈↓↓␈↓ ∧xx ← ␈↓¬(A B)␈↓↓;
␈↓ ↓H␈↓↓␈↓ ∧xy ← ␈↓¬(A B)␈↓↓;
␈↓ ↓H␈↓↓␈↓ ∧xz ← [␈↓αad|␈↓↓x ← ␈↓¬C␈↓↓] ]

␈↓ ↓H␈↓↓␈↓ βxtest2[] ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[]
␈↓ ↓H␈↓↓␈↓ ∧xx ← ␈↓¬(A B)␈↓↓;
␈↓ ↓H␈↓↓␈↓ ∧xy ← x;
␈↓ ↓H␈↓↓␈↓ ∧xz ← [␈↓αad|␈↓↓x ← ␈↓¬C␈↓↓]]


␈↓ ↓H␈↓After␈αexecuting␈α␈↓↓test1␈↓␈αwe␈αhave␈α␈↓↓x=␈↓¬(A C)␈↓↓␈↓,␈α␈↓↓y=␈↓¬(A B)␈↓↓␈↓,␈αand␈α␈↓↓z=␈↓¬(C)␈↓↓␈↓,␈αwhile␈αafter␈αexecuting␈α␈↓↓test2␈↓␈αwe␈αhave
␈↓ ↓H␈↓␈↓↓x=␈↓¬(A C)␈↓↓␈↓,␈α⊃␈↓↓y=␈↓¬(A C)␈↓↓␈↓,␈α∩and␈α⊃␈↓↓z=␈↓¬(C)␈↓↓␈↓.␈α⊃ This␈α∩is␈α⊃because␈α∩in␈α⊃the␈α⊃first␈α∩case␈α⊃␈↓↓x␈↓␈α⊃and␈α∩␈↓↓y␈↓␈α⊃are␈α∩different␈α⊃list-
␈↓ ↓H␈↓structures,␈αso␈αmodifying␈αdoesn't␈αchange␈α␈↓↓y.␈↓␈αIn␈αthe␈αsecond␈αcase␈α␈↓↓x␈↓␈αand␈α␈↓↓y␈↓␈αare␈αthe␈αsame␈αlist-structures
␈↓ ↓H␈↓(e.g.␈α␈↓↓x ␈↓αeq␈↓↓ y␈↓)␈αso␈αany␈αmodification␈αof␈α␈↓↓x␈↓␈α
also␈αaffects␈α␈↓↓y.␈↓␈α In␈αfact␈αif␈α␈↓↓x␈↓␈α
and␈α␈↓↓y␈↓␈αare␈αnot␈αthe␈αsame,␈αbut␈α
share
␈↓ ↓H␈↓some␈αsubstructure,␈αany␈αmodification␈αof␈αthat␈αstructure␈αwill␈αaffect␈αboth␈α␈↓↓x␈↓␈αand␈α␈↓↓y.␈↓␈α The␈αuncleanliness
␈↓ ↓H␈↓of ␈↓¬RPLACA ␈↓and ␈↓¬RPLACD ␈↓means that the programmer must know exactly what list structures merge.

␈↓ ↓H␈↓Here are the internal forms of the test programs in case you are unsure of the notations:
␈↓ ↓H␈↓116␈↓ ¬|Chapter  V␈↓ H



␈↓ ↓H␈↓¬␈↓ β8(DEFUN TEST1 ()
␈↓ ↓H␈↓¬␈↓ β8  (PROG ()
␈↓ ↓H␈↓¬␈↓ β8    (SETQ X '(A B))
␈↓ ↓H␈↓¬␈↓ β8    (SETQ Y '(A B))
␈↓ ↓H␈↓¬␈↓ β8    (SETQ Z (RPLACA (CDR X) 'C))
␈↓ ↓H␈↓¬␈↓ β8  ))

␈↓ ↓H␈↓¬␈↓ β8(DEFUN TEST2 ()
␈↓ ↓H␈↓¬␈↓ β8  (PROG ()
␈↓ ↓H␈↓¬␈↓ β8    (SETQ X '(A B))
␈↓ ↓H␈↓¬␈↓ β8    (SETQ Y X)
␈↓ ↓H␈↓¬␈↓ β8    (SETQ Z (RPLACA (CDR X) 'C))
␈↓ ↓H␈↓¬␈↓ β8  ))


␈↓ ↓H␈↓A typical application is the pseudo-function ␈↓↓nconc␈↓ defined by


␈↓ ↓H␈↓↓␈↓ βxnconc[u,v] ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[w]
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ v;
␈↓ ↓H␈↓↓␈↓ ∧xw ← u;
␈↓ ↓H␈↓↓4.1)␈↓ ∧8loop:
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓w ␈↓αthen␈↓↓ [␈↓αd|␈↓↓w ← v; ␈↓αreturn␈↓↓ u];
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓αd|␈↓↓w;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ loop].


␈↓ ↓H␈↓␈↓↓nconc␈↓ can also be given a definition that has the form of a recursive program, namely

␈↓ ↓H␈↓↓␈↓ βxnconc[u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ nconc1[u, u, v]
␈↓ ↓H␈↓↓4.2)
␈↓ ↓H␈↓↓␈↓ βxnconc1[u, w, v] ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓w ␈↓αthen␈↓↓ [λside: u][rplacd[w, v]]
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αelse␈↓↓ nconc1[u, ␈↓αd|␈↓↓w, v].


␈↓ ↓H␈↓        The␈α∂value␈α∂of␈α∞␈↓↓nconc[u, v]␈↓␈α∂is␈α∂just␈α∞␈↓↓u * v,␈↓␈α∂but␈α∂it␈α∞achieves␈α∂this␈α∂result␈α∞by␈α∂modifying␈α∂the␈α∞last
␈↓ ↓H␈↓element␈α
of␈α
␈↓↓u␈↓␈α
to␈α
point␈α
to␈α␈↓↓v.␈↓␈α
 It␈α
is␈α
typically␈α
used␈α
when␈αno␈α
other␈α
variable␈α
points␈α
to␈α
a␈α
list␈αstructure
␈↓ ↓H␈↓that␈α⊃merges␈α⊃into␈α∩␈↓↓u,␈↓␈α⊃and␈α⊃the␈α∩old␈α⊃value␈α⊃of␈α∩␈↓↓u␈↓␈α⊃will␈α⊃not␈α∩be␈α⊃used␈α⊃again.␈α∩ In␈α⊃that␈α⊃case,␈α∩␈↓↓nconc␈↓␈α⊃is
␈↓ ↓H␈↓advantageous,␈α∂because␈α∂it␈α⊂does␈α∂no␈α∂␈↓↓cons␈↓es,␈α⊂and␈α∂thus␈α∂can't␈α⊂initiate␈α∂garbage␈α∂collection.␈α⊂ No␈α∂formal
␈↓ ↓H␈↓methods exist at present for proving that these conditions are met.

␈↓ ↓H␈↓        ␈↓¬RPLACD␈α⊃␈↓can␈α⊃also␈α∩be␈α⊃used␈α⊃to␈α⊃insert␈α∩an␈α⊃element␈α⊃into␈α⊃the␈α∩middle␈α⊃of␈α⊃a␈α⊃list.␈α∩ Suppose,␈α⊃for
␈↓ ↓H␈↓example,␈αthat␈αwe␈αwish␈αto␈αinsert␈αthe␈αatom␈α␈↓¬B␈α␈↓after␈αevery␈αoccurrence␈αof␈αthe␈αatom␈α␈↓¬A␈α␈↓in␈αa␈αlist␈α␈↓↓u.␈↓␈α We
␈↓ ↓H␈↓can␈αdefine␈αa␈αpure␈αLISP␈αfunction␈αthat␈αgives␈αa␈αlist␈αobtained␈αfrom␈αthe␈αlist␈α␈↓↓u␈↓␈αby␈αinserting␈αsuch␈α␈↓¬B␈↓'s␈αas
␈↓ ↓H␈↓follows:

␈↓ ↓H␈↓4.3)␈↓ ↓n␈↓↓    insertb u ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬NIL␈↓↓ ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u = ␈↓¬A ␈↓↓␈↓αthen␈↓↓ ␈↓¬A ␈↓↓. [␈↓¬B ␈↓↓. insertb ␈↓αd|␈↓↓u] ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u . insertb ␈↓αd|␈↓↓u.␈↓ 
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠117


␈↓ ↓H␈↓Computing␈α␈↓↓insertb u␈↓␈αdoes␈αnot␈αchange␈αthe␈αvalue␈αof␈α␈↓↓u,␈↓␈αbecause␈αnew␈αlist␈αstructure␈αis␈αmanufactured.
␈↓ ↓H␈↓On the other hand, if we define


␈↓ ↓H␈↓↓␈↓ βxinsertb u ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[w];
␈↓ ↓H␈↓↓␈↓ ∧xw ← u;
␈↓ ↓H␈↓↓4.4)␈↓ ∧8loop:
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓w ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ u;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ ␈↓αa|␈↓↓w = ␈↓¬A ␈↓↓␈↓αthen␈↓↓ [␈↓αd|␈↓↓w ← ␈↓¬B ␈↓↓. ␈↓αd|␈↓↓w; w ← ␈↓αd|␈↓↓w];
␈↓ ↓H␈↓↓␈↓ ∧xw ← ␈↓αd|␈↓↓w;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ loop],


␈↓ ↓H␈↓we␈αget␈αa␈αprogram␈αthat␈αdoes␈αthe␈αactual␈αinsertions.␈α It␈αis␈αmore␈αefficient␈αbecause␈αit␈αuses␈αfewer␈α␈↓↓cons␈↓es,
␈↓ ↓H␈↓but␈α⊂it␈α⊂will␈α⊂damage␈α∂any␈α⊂list␈α⊂structure␈α⊂that␈α∂merges␈α⊂with␈α⊂the␈α⊂structure␈α∂representing␈α⊂␈↓↓u,␈↓␈α⊂and␈α⊂it␈α∂is
␈↓ ↓H␈↓mathematically recalcitrant.

␈↓ ↓H␈↓        Finally␈α
we␈α
have␈α
two␈α
examples␈α
of␈α∞programs␈α
destructively␈α
removing␈α
items␈α
from␈α
a␈α∞list.␈α
 The
␈↓ ↓H␈↓program␈α⊂␈↓↓remv␈↓␈α⊂deletes␈α⊂all␈α∂occurrences␈α⊂of␈α⊂␈↓↓x␈↓␈α⊂from␈α∂the␈α⊂list␈α⊂␈↓↓u.␈↓␈α⊂ To␈α∂remove␈α⊂something␈α⊂from␈α⊂a␈α∂list
␈↓ ↓H␈↓destructively␈α∂the␈α∞␈↓↓cdr␈↓␈α∂of␈α∂the␈α∞previous␈α∂element␈α∂is␈α∞rplaced␈α∂by␈α∞the␈α∂pointer␈α∂to␈α∞the␈α∂next␈α∂element.␈α∞ If
␈↓ ↓H␈↓there is no previous element, the list is replaced by its ␈↓↓cdr.␈↓  Thus the two loops of ␈↓↓remv.␈↓

␈↓ ↓H␈↓↓␈↓ βxremv[x,u]
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[v]
␈↓ ↓H␈↓↓␈↓ ∧8␈↓¬LEADERS␈↓↓:
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ u;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ ␈↓αa|␈↓↓u = x ␈↓αthen␈↓↓ [u ← ␈↓αd|␈↓↓u, ␈↓αgo to␈↓↓ ␈↓¬LEADERS␈↓↓];
␈↓ ↓H␈↓↓␈↓ ∧xv ← u;
␈↓ ↓H␈↓↓␈↓ ∧8␈↓¬INTERIORS:␈↓↓
␈↓ ↓H␈↓↓4.5)␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓v ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ u;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ [␈↓αad|␈↓↓v = x] ␈↓αthen␈↓↓ [␈↓αd|␈↓↓v ← ␈↓αdd|␈↓↓v; ␈↓αgo to␈↓↓  ␈↓¬INTERIORS␈↓↓];
␈↓ ↓H␈↓↓␈↓ ∧xv ← ␈↓αd|␈↓↓v;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ ␈↓¬INTERIORS␈↓↓]

␈↓ ↓H␈↓The␈αprogram␈α␈↓↓prune␈↓␈αdeletes␈α
all␈αelements␈αof␈αthe␈αlist␈α
␈↓↓u␈↓␈αthat␈αare␈αmembers␈α
of␈αthe␈αlist␈α␈↓↓seen.␈↓␈α Instead␈α
of
␈↓ ↓H␈↓using two loops, we tack a dummy onto ␈↓↓u␈↓ so there will always be a previous element.


␈↓ ↓H␈↓↓␈↓ βxprune[u,seen]
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[v]
␈↓ ↓H␈↓↓␈↓ ∧xu ← ␈↓¬NIL␈↓↓ . u;
␈↓ ↓H␈↓↓␈↓ ∧xv ← u;
␈↓ ↓H␈↓↓␈↓ ∧8ploop:
␈↓ ↓H␈↓↓4.6)␈↓ ∧x␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓v ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ ␈↓αd|␈↓↓u;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αif␈↓↓ [␈↓αad|␈↓↓v ε seen] ␈↓αthen␈↓↓ [␈↓αd|␈↓↓v ← ␈↓αdd|␈↓↓v; ␈↓αgo to␈↓↓  ploop];
␈↓ ↓H␈↓↓␈↓ ∧xv ← ␈↓αd|␈↓↓v;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αgo to␈↓↓ ploop]

␈↓ ↓H␈↓In␈αorder␈αto␈αmake␈α
the␈αremovals␈αgo␈αuniformly␈α
we␈αbegin␈αby␈αtacking␈α
a␈αdummy␈αelement␈α␈↓¬NIL␈↓␈α
onto␈α␈↓↓u.␈↓
␈↓ ↓H␈↓118␈↓ ¬|Chapter  V␈↓ H


␈↓ ↓H␈↓Thus␈α∞the␈α∂list␈α∞is␈α∂non-empty␈α∞and␈α∂the␈α∞next␈α∂element␈α∞of␈α∂interest␈α∞is␈α∂the␈α∞"second"␈α∂element␈α∞of␈α∂the␈α∞list
␈↓ ↓H␈↓currently␈αpointed␈α
to.␈α An␈αexample␈α
of␈αa␈αprogram␈α
using␈αdestructive␈αoperations␈α
in␈αa␈α
controlled␈αand
␈↓ ↓H␈↓"clean"␈α∂manner␈α⊂is␈α∂the␈α∂␈↓↓editor␈↓␈α⊂given␈α∂in␈α⊂Chapter␈α∂VI.␈α∂ Here␈α⊂the␈α∂program␈α∂to␈α⊂be␈α∂edited␈α⊂is␈α∂initially
␈↓ ↓H␈↓copied, then destructively edited to save repeated and unnecessary reconstruction.

␈↓ ↓H␈↓α␈↓ ε⊂Exercise

␈↓ ↓H␈↓1. Consider the following test programs:

␈↓ ↓H␈↓↓␈↓ βxtest3[] ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[]
␈↓ ↓H␈↓↓␈↓ ∧xx ← ␈↓¬(A B)␈↓↓;
␈↓ ↓H␈↓↓␈↓ ∧xy ← x;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αreturn␈↓↓ x ␈↓αequal␈↓↓ [λside.x][␈↓αa|␈↓↓y ← ␈↓¬C␈↓↓]]

␈↓ ↓H␈↓↓␈↓ βxtest4[] ←
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αprogram␈↓↓[[]
␈↓ ↓H␈↓↓␈↓ ∧xx ← ␈↓¬(A B)␈↓↓;
␈↓ ↓H␈↓↓␈↓ ∧xy ← x;
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αreturn␈↓↓ copy x ␈↓αequal␈↓↓ [λside.x][␈↓αa|␈↓↓y ← ␈↓¬C␈↓↓]]

␈↓ ↓H␈↓↓␈↓ βxcopy x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ [copy car x . copy cdr x]


␈↓ ↓H␈↓We have ␈↓↓copy x ␈↓αequal␈↓↓ x =␈↓¬T␈↓↓␈↓ and ␈↓↓test3[]=␈↓¬T␈↓↓␈↓ but ␈↓↓test4[]=␈↓¬NIL␈↓↓␈↓.  Explain this.



␈↓ ↓H␈↓5.  ␈↓αRe-entrant List Structure.␈↓


␈↓ ↓H␈↓        So␈α
far␈αwe␈α
have␈α
only␈αconsidered␈α
list␈α
structure␈αin␈α
which␈α
all␈α␈↓↓car-cdr␈↓␈α
chains␈α
terminate␈αin␈α
atoms.
␈↓ ↓H␈↓Mathematically,␈αinfinite␈αlist␈αstructures␈αare␈αalso␈αpossible.␈α They␈αcan't␈αbe␈αrepresented␈αin␈αa␈αcomputer,
␈↓ ↓H␈↓but␈α⊃since␈α⊃they␈α⊃can␈α⊃satisfy␈α⊃the␈α⊃LISP␈α∩algebraic␈α⊃axioms,␈α⊃we␈α⊃have␈α⊃had␈α⊃to␈α⊃exclude␈α⊃them␈α∩in␈α⊃our
␈↓ ↓H␈↓axiomatization␈αof␈α
LISP␈αby␈αaxiom␈α
schemata␈αof␈α
induction.␈α Another␈αpossibility␈α
is␈αthe␈αre-entrant␈α
list
␈↓ ↓H␈↓structure.␈α∂ These␈α∂can␈α∂be␈α∂created␈α∂by␈α∂the␈α∂␈↓¬RPLACA␈α∂␈↓and␈α∂␈↓¬RPLACD␈α∂␈↓operations.␈α∂ Figure␈α∂9␈α∂shows␈α∂some
␈↓ ↓H␈↓examples␈α∞of␈α
re-entrant␈α∞list␈α
structures.␈α∞ For␈α∞example,␈α
if␈α∞the␈α
variable␈α∞␈↓↓x␈↓␈α
has␈α∞value␈α∞␈↓¬(A),␈α
␈↓executing
␈↓ ↓H␈↓the␈α
statement␈α
␈↓↓␈↓αa|␈↓↓x␈α
←␈α
x␈↓␈α
gives␈α
rise␈α
to␈α
a␈α
circular␈α
structure␈α
(i)␈α
while␈α
if␈α
we␈α
execute␈α
␈↓↓␈↓αd|␈↓↓x␈α
←␈α
x␈↓␈α
we␈α
get␈αthe
␈↓ ↓H␈↓structure␈α(ii).␈α If␈αwe␈αexecute␈α␈↓↓␈↓αd|␈↓↓x␈α←␈αx␈↓␈αwith␈α␈↓↓x␈↓␈αas␈αin␈α(i)␈αor␈α␈↓↓␈↓αa|␈↓↓x␈α←␈αx␈↓␈αwith␈α␈↓↓x␈↓␈αas␈αin␈α(ii)␈αwe␈αget␈αthe␈αdoubly
␈↓ ↓H␈↓re-entrant␈αstructure␈α(iii).␈α  Notice␈αthat␈αin␈α(i)␈αwe␈αhave␈α␈↓↓x ␈↓αeq␈↓↓ ␈↓αa|␈↓↓x␈↓,␈αin␈α(ii)␈α␈↓↓x ␈↓αeq␈↓↓ ␈↓αd|␈↓↓x␈↓␈αand␈αin␈α(iii)␈αwe␈αhave
␈↓ ↓H␈↓␈↓↓x ␈↓αeq␈↓↓ ␈↓αa|␈↓↓x␈↓␈α∀and␈α∃␈↓↓x ␈↓αeq␈↓↓ ␈↓αd|␈↓↓x␈↓.␈α∀  These␈α∀equation␈α∃contradict␈α∀the␈α∀induction␈α∃schemata␈α∀for␈α∀lists␈α∃and␈α∀S-
␈↓ ↓H␈↓expressions.␈α Thus␈αlist␈αstructures␈αwith␈αdestructive␈αoperations␈αare␈αnot␈αa␈αmodel␈αof␈αthe␈αtheory␈αof␈αlists
␈↓ ↓H␈↓and S-expressions given in Chapter III.

␈↓ ↓H␈↓        We␈α
can␈α
simulate␈αrecursive␈α
programs␈α
by␈αreplacing␈α
calls␈α
to␈αthe␈α
program␈α
by␈αa␈α
pointer␈α
to␈αthe
␈↓ ↓H␈↓lambda␈α∞expression␈α∞defining␈α∂the␈α∞program.␈α∞ For␈α∂example␈α∞if␈α∞we␈α∂execute␈α∞␈↓↓mkleft␈↓␈α∞then␈α∂the␈α∞resulting
␈↓ ↓H␈↓value of ␈↓↓left␈↓ will have the structure shown in Figure 10.
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠119




␈↓ ↓H␈↓↓␈↓ β(mkleft[]
␈↓ ↓H␈↓↓␈↓ βh␈↓αprogram␈↓↓[[]
␈↓ ↓H␈↓↓␈↓ ∧(left ← ␈↓¬(LAMBDA (X) (COND ((ATOM X) X) (T (LEFT (CAR X))) ))␈↓↓;
␈↓ ↓H␈↓↓␈↓ ∧(␈↓αa|␈↓↓␈↓αad|␈↓↓␈↓αadd|␈↓↓␈↓αadd|␈↓↓left ← left;
␈↓ ↓H␈↓↓␈↓ ∧(␈↓αreturn␈↓↓ ␈↓¬LEFT␈↓↓]


␈↓ ↓H␈↓Also ␈↓↓apply[left <x>] = left[x]␈↓ where

␈↓ ↓H␈↓5.1) ␈↓ ∧l␈↓↓left x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ left ␈↓αa|␈↓↓x␈↓. 

␈↓ ↓H␈↓The␈αreason␈αfor␈α
encasing␈αthe␈αconstruction␈α
in␈αa␈α␈↓αprogram␈↓␈αeven␈α
though␈αthe␈αassignments␈α
were␈αmade
␈↓ ↓H␈↓to␈α∞non-prog␈α∞variables␈α∂was␈α∞that␈α∞most␈α∂LISPs␈α∞(including␈α∞MACLISP)␈α∂do␈α∞not␈α∞check␈α∂for␈α∞re-entrant
␈↓ ↓H␈↓structures and an attempt to print a re-entrant list will cause an infinite loop.
␈↓"
␈↓"␈↓ ↓H␈↓        ⊂αααπααα⊃                   ⊂αααπααα⊃                       ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓  ααααα→~   ~   ~             ααααα→~   ~   ~              αααααααα→~   ~   ~
␈↓"␈↓ ↓H␈↓    ↑   %απα∀απα$               ↑   %απα∀απα$                ↑  ↑   %απα∀απα$
␈↓"␈↓ ↓H␈↓    ~     ~   ↓                 ~     ↓   ~                  ~  ~     ~   ~
␈↓"␈↓ ↓H␈↓    %ααααα$  NIL                ~     A   ~                  ~  %ααααα$   ~
␈↓"␈↓ ↓H␈↓                                %ααααααααα$                  %αααααααααααα$
␈↓"
␈↓"
␈↓"␈↓ ↓H␈↓    (RPLACA X X)                (RPLACD X X)             (RPLACA (RPLACDA X X))
␈↓"
␈↓"␈↓ ↓H␈↓         (i)                        (ii)                          (iii)
␈↓"
␈↓ ↓H␈↓␈↓ ∧∂␈↓αFigure 9.␈↓  Examples of re-entrant list structures.
␈↓ ↓H␈↓120␈↓ ¬|Chapter  V␈↓ H


␈↓"
␈↓"␈↓ ↓H␈↓    ⊂αααπααα⊃   ⊂αααπααα⊃   ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ααα→~   ~   εαα→~   ~   εαα→~   ~   εαα→NIL
␈↓"␈↓ ↓H␈↓ ↑  %απα∀ααα$   %απα∀ααα$   %απα∀ααα$
␈↓"␈↓ ↓H␈↓ ~    ↓           ↓           ~   
␈↓"␈↓ ↓H␈↓ ~ LAMBDA       ⊂αααπααα⊃     ~  
␈↓"␈↓ ↓H␈↓ ~              ~   ~   ~     ~ 
␈↓"␈↓ ↓H␈↓ ~              %απα∀απα$     ~ 
␈↓"␈↓ ↓H␈↓ ~                ↓   ↓       ~ 
␈↓"␈↓ ↓H␈↓ ~                X  NIL      ~ 
␈↓"␈↓ ↓H␈↓ ~                            ~
␈↓"␈↓ ↓H␈↓ ~           ⊂αααααααααααααααα$ 
␈↓"␈↓ ↓H␈↓ ~           ↓
␈↓"␈↓ ↓H␈↓ ~         ⊂αααπααα⊃   ⊂αααπααα⊃   ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ~         ~   ~   εαα→~   ~   εαα→~   ~   εαα→NIL
␈↓"␈↓ ↓H␈↓ ~         %απα∀ααα$   %απα∀ααα$   %απα∀ααα$
␈↓"␈↓ ↓H␈↓ ~           ↓           ~           ↓
␈↓"␈↓ ↓H␈↓ ~         COND          ~         ⊂αααπααα⊃   ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ~                       ~         ~   ~   εαα→~   ~   εαα→NIL
␈↓"␈↓ ↓H␈↓ ~                       ~         %απα∀ααα$   %απα∀ααα$
␈↓"␈↓ ↓H␈↓ ~                       ~           ↓           ↓
␈↓"␈↓ ↓H␈↓ ~                       ~           T         ⊂αααπααα⊃   ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ~                       ↓                     ~   ~   εαα→~   ~   εαα→NIL
␈↓"␈↓ ↓H␈↓ ~                     ⊂αααπααα⊃   ⊂αααπααα⊃   %απα∀ααα$   %απα∀ααα$
␈↓"␈↓ ↓H␈↓ ~                     ~   ~   εαα→~   ~   ~     ~           ↓
␈↓"␈↓ ↓H␈↓ ~                     %απα∀ααα$   %απα∀απα$     ~         ⊂αααπααα⊃   ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓ ~                       ~           ↓   ↓       ~         ~   ~   εαα→~   ~   ~
␈↓"␈↓ ↓H␈↓ ~                       ~           X  NIL      ~         %απα∀ααα$   %απα∀απα$
␈↓"␈↓ ↓H␈↓ ~                       ↓                       ~           ↓           ↓   ↓ 
␈↓"␈↓ ↓H␈↓ ~                     ⊂αααπααα⊃   ⊂αααπααα⊃     ~          CAR          X  NIL
␈↓"␈↓ ↓H␈↓ ~                     ~   ~   εαα→~   ~   ~     ~
␈↓"␈↓ ↓H␈↓ ~                     %απα∀ααα$   %απα∀απα$     ~
␈↓"␈↓ ↓H␈↓ ~                       ↓           ↓   ↓       ~
␈↓"␈↓ ↓H␈↓ ~                      ATOM         X  NIL      ~
␈↓"␈↓ ↓H␈↓ ~                                               ~
␈↓"␈↓ ↓H␈↓ ~                                               ~
␈↓"␈↓ ↓H␈↓ %ααααααααααααααααααααααααααααααααααααααααααααααα$ 

␈↓ ↓H␈↓␈↓ α@␈↓↓␈↓¬(SETQ LEFT '(LAMBDA (X) (COND ((ATOM X) X) (T (LEFT (CAR X))) )))␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧␈↓↓␈↓¬(RPLACA (CADR (CADDR (CADDR LEFT))) LEFT)␈↓↓␈↓

␈↓ ↓H␈↓␈↓ ∧)␈↓αFigure 10.␈↓  Another re-entrant list structure.



␈↓ ↓H␈↓        As␈α
we␈αhave␈α
just␈αnoted,␈α
re-entrant␈αlist␈α
structures␈αdo␈α
not␈αsatisfy␈α
the␈αinduction␈α
axioms␈αgiven
␈↓ ↓H␈↓for␈α
S-expressions,␈α
and␈α
the␈α
correctness␈α
of␈α
most␈α
of␈α
programs␈α
given␈α
in␈α
this␈α
book␈α
depends␈α
on␈αthe␈α
data
␈↓ ↓H␈↓being␈α
non-re-entrant.␈α
 Re-entrant␈α
structures␈αcan␈α
be␈α
represented␈α
by␈α
S-expressions␈αprovided␈α
certain
␈↓ ↓H␈↓atoms␈α⊃are␈α⊂reserved␈α⊃for␈α⊃use␈α⊂as␈α⊃labels␈α⊃and␈α⊂a␈α⊃suitable␈α⊃convention␈α⊂is␈α⊃adopted␈α⊃for␈α⊂distinguishing
␈↓ ↓H␈↓ordinary␈αatoms␈αfrom␈αlabels␈αand␈αpointers␈αto␈αthe␈αlabels.␈α For␈αexample,␈αthe␈αstructure␈αof␈αfigure␈α9␈α(iii)
␈↓ ↓H␈↓might be represented by ␈↓¬((LABEL A).((POINT A).(POINT A)))␈↓.
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠121


␈↓ ↓H␈↓        Programs␈α∞that␈α
compute␈α∞with␈α∞re-entrant␈α
structures␈α∞need␈α
to␈α∞keep␈α∞lists␈α
of␈α∞vertices␈α∞they␈α
have
␈↓ ↓H␈↓visited␈α
so␈α
that␈αthey␈α
can␈α
tell␈αwhen␈α
they␈α
are␈α
about␈αto␈α
re-enter.␈α
 Thus␈αthe␈α
equivalence␈α
of␈αmerging␈α
list
␈↓ ↓H␈↓structure can be tested by the predicate ␈↓↓equiv␈↓ defined by:


␈↓ ↓H␈↓↓␈↓ β8equiv[x, y] ← ¬[equiv1[x, y, ␈↓¬NIL␈↓↓] ␈↓αeq␈↓↓ ␈↓¬LOSE␈↓↓]

␈↓ ↓H␈↓↓␈↓ β8equiv1[x, y, u] ←
␈↓ ↓H␈↓↓␈↓ βx␈↓αif␈↓↓ u ␈↓αeq␈↓↓ ␈↓¬LOSE␈↓↓ ␈↓αthen␈↓↓ ␈↓¬LOSE␈↓↓
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ x ␈↓αeq␈↓↓ y ∨ match[x, y, u] ␈↓αthen␈↓↓ u
␈↓ ↓H␈↓↓5.2)␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ∨ ␈↓αat|␈↓↓y ∨ unmatch[x, y, u] ␈↓αthen␈↓↓ ␈↓¬LOSE␈↓↓
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ equiv1[␈↓αa|␈↓↓x, ␈↓αa|␈↓↓y, equiv1[␈↓αd|␈↓↓x, ␈↓αd|␈↓↓y, [x . y] . u]]

␈↓ ↓H␈↓↓␈↓ β8match[x, y, u] ← ¬␈↓αn|␈↓↓u ∧ [[x ␈↓αeq␈↓↓ ␈↓αaa|␈↓↓u ∧ y ␈↓αeq␈↓↓ ␈↓αda|␈↓↓u] ∨ match[x, y, ␈↓αd|␈↓↓u]]

␈↓ ↓H␈↓↓␈↓ β8unmatch[x, y, u] ← ¬␈↓αn|␈↓↓u ∧ [x ␈↓αeq␈↓↓ ␈↓αaa|␈↓↓u ∨ y ␈↓αeq␈↓↓ ␈↓αda|␈↓↓u ∨ unmatch[x, y, ␈↓αd|␈↓↓u]]

␈↓ ↓H␈↓The␈αargument␈α␈↓↓u␈↓␈αin␈α␈↓↓equiv1␈↓␈αis␈αa␈αlist␈αof␈αpositions␈αpairs␈αseen␈αso␈αfar␈αin␈αthe␈αparallel␈αtraversal␈αof␈α␈↓↓x␈↓␈αand
␈↓ ↓H␈↓␈↓↓y.␈↓␈α It␈αis␈αnecessary␈αto␈αcarry␈αaround␈αinformation␈αof␈αthis␈αsort␈αin␈αorder␈αto␈αavoid␈αrepeating␈αsome␈αpart
␈↓ ↓H␈↓of␈αthe␈αcomputation␈αforever.␈α Note␈αalso␈αthe␈αuse␈αof␈α␈↓αeq␈↓.␈α The␈αtest␈αwe␈αwant␈αto␈αmake␈αis␈α"have␈αwe␈αbeen
␈↓ ↓H␈↓here␈α
before?"␈α
not␈α∞"have␈α
we␈α
seen␈α
this␈α∞S-expression␈α
before?"␈α
as␈α
the␈α∞program␈α
␈↓↓equal␈↓␈α
for␈α∞testing␈α
the
␈↓ ↓H␈↓latter would loop on re-entrant structures.


␈↓ ↓H␈↓α␈↓ ε⊂Exercise

␈↓ ↓H␈↓1.  Consider the following definition of the fibonnaci function.

␈↓ ↓H␈↓␈↓ α8␈↓↓        fibon n ← ␈↓αif␈↓↓ n = ␈↓¬0 ␈↓↓∨ n = ␈↓¬1 ␈↓↓␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ fibloop[n, ␈↓¬(1 1)␈↓↓]␈↓
␈↓ ↓H␈↓5.3)
␈↓ ↓H␈↓␈↓ α8␈↓↓        fibloop[n, l] ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓            ␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αdd|␈↓↓l ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                [␈↓αif␈↓↓ n = ␈↓¬2 ␈↓↓␈↓αthen␈↓↓ ␈↓αad|␈↓↓rplacd[␈↓αd|␈↓↓l, <␈↓αa|␈↓↓l + ␈↓αad|␈↓↓l>]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αelse␈↓↓ fibloop[sub1 n, rplacd[␈↓αd|␈↓↓l, <␈↓αa|␈↓↓l + ␈↓αad|␈↓↓l>]]]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓            ␈↓αelse␈↓↓ ␈↓αif␈↓↓ n = ␈↓¬2 ␈↓↓␈↓αthen␈↓↓ ␈↓αadd|␈↓↓l ␈↓αelse␈↓↓ fibloop[n-␈↓¬1, ␈↓↓␈↓αd|␈↓↓l]␈↓

␈↓ ↓H␈↓After␈αevaluating␈α␈↓↓fibon␈α5␈↓␈α if␈αwe␈αexamine␈αthe␈αdefinition␈αon␈αthe␈αproperty␈αlist␈αof␈α␈↓↓fibon␈↓␈α it␈α
corresponds
␈↓ ↓H␈↓to the definition

␈↓ ↓H␈↓␈↓ β␈↓↓fibon n ← ␈↓αif␈↓↓ n = ␈↓¬0 ␈↓↓∨ n = ␈↓¬1 ␈↓↓␈↓αthen␈↓↓ ␈↓¬1 ␈↓↓␈↓αelse␈↓↓ fibloop[n, ␈↓¬(1 1 2 3 5 8)␈↓↓]␈↓ . 

␈↓ ↓H␈↓Thus ␈↓↓fibon␈↓ seems to be getting smarter.  What has happened?



␈↓ ↓H␈↓6.  ␈↓αExercises.␈↓
␈↓ ↓H␈↓122␈↓ ¬|Chapter  V␈↓ H


␈↓ ↓H␈↓α␈↓ ¬=Sequential programs

␈↓ ↓H␈↓1.␈α
 Extend␈α∞␈↓↓eval␈↓␈α
(I.13.1)␈α
to␈α∞handle␈α
␈↓¬PROG,␈α
␈↓␈↓¬SETQ,␈α∞␈↓␈↓¬GO,␈α
␈↓and␈α
␈↓¬RETURN.␈α∞␈↓␈α
Is␈α
it␈α∞necessary␈α
to␈α∞change␈α
the
␈↓ ↓H␈↓evaluation␈α
of␈α
␈↓¬COND␈↓s?␈α  There␈α
are␈α
several␈αpossible␈α
ways␈α
of␈αhandling␈α
labels.␈α
 Try␈αto␈α
think␈α
of␈αat␈α
least
␈↓ ↓H␈↓two and consider the advantages and disadvantages of each.

␈↓ ↓H␈↓α␈↓ ∧UProperty list and Special properties.

␈↓ ↓H␈↓2.␈α∂ The␈α∂association-list␈α∞argument␈α∂of␈α∂␈↓↓eval␈↓␈α∂I.13.1␈α∞provides␈α∂a␈α∂way␈α∞of␈α∂associating␈α∂a␈α∂value␈α∞property
␈↓ ↓H␈↓with␈αany␈α
atom.␈α This␈αsimple␈α
version␈αof␈α
␈↓↓eval␈↓␈αdoes␈αnot␈α
distinguish␈αbetween␈α
values␈αthat␈αare␈α
intended
␈↓ ↓H␈↓to␈α∂be␈α∂applied␈α∂as␈α∂functions␈α∂and␈α∂simple␈α⊂values.␈α∂ Replace␈α∂the␈α∂a-list␈α∂argument␈α∂by␈α∂a␈α⊂more␈α∂general
␈↓ ↓H␈↓structure␈α∞capable␈α∞of␈α∞associating␈α∞arbitrary␈α∂properties␈α∞with␈α∞an␈α∞atom.␈α∞ Write␈α∂appropriate␈α∞accessing
␈↓ ↓H␈↓and␈α
modifying␈α
functions␈α
(␈↓↓get,␈↓␈α
␈↓↓putprop,␈↓␈α
and␈α
␈↓↓remprop␈↓)␈α
and␈α
modify␈α
␈↓↓eval␈↓␈α
where␈αnecessary,␈α
including
␈↓ ↓H␈↓a clause for ␈↓¬DEFUN. ␈↓

␈↓ ↓H␈↓3.␈α∀ How␈α∪might␈α∀you␈α∪extend␈α∀the␈α∪more␈α∀sophisticated␈α∪␈↓↓eval␈↓␈α∀above␈α∪to␈α∀handle␈α∪␈↓¬PROG␈α∀␈↓and␈α∪related
␈↓ ↓H␈↓constructs.  Are their now new ways of treating statement labels?

␈↓ ↓H␈↓α␈↓ ¬PMacro definitions

␈↓ ↓H␈↓4.␈α The␈αmacro␈αdefinition␈αfeature␈αof␈αLISP␈αdescribed␈αearlier␈αis␈αsome␈αwhat␈αclumsy␈αto␈αuse␈αin␈αgeneral.
␈↓ ↓H␈↓Write␈αyour␈αown␈αmacro␈αdefinition␈αmaker␈αthat␈αtakes␈αa␈αmacro␈αname,␈αa␈αparmeter␈αspecification␈αand␈αa
␈↓ ↓H␈↓definition␈αbody␈αand␈αproduces␈α an␈αappropriate␈αdefinition.␈α The␈αparameter␈αspecification␈αcould␈αbe␈αa
␈↓ ↓H␈↓pattern␈α∪containing␈α∩variables␈α∪that␈α∩are␈α∪to␈α∩match␈α∪various␈α∩kinds␈α∪of␈α∩expressions␈α∪such␈α∪as␈α∩atoms,
␈↓ ↓H␈↓segments␈αof␈αlists,␈αarbitrary␈αexpressions,␈αexpressions␈αsatisfying␈αsome␈αpredicate.␈α The␈α
pattern␈αwould
␈↓ ↓H␈↓also␈α
contain␈α
constants,␈α
etc..␈α
 The␈α
macro␈αdefinition␈α
produced␈α
would␈α
then␈α
generate␈α
code␈αby␈α
matching
␈↓ ↓H␈↓the␈αcall␈αto␈αthe␈αpattern␈αto␈αobtain␈αvalues␈αof␈αthe␈αvariables␈αand␈αthen␈αfilling␈αthem␈αin␈αwhere␈αthey␈αoccur
␈↓ ↓H␈↓in the body.  For example given a definition such as

␈↓ ↓H␈↓¬␈↓ βλ(MACRO-MAKER FOR (?I IN ??LIST DO *STMTS)
␈↓ ↓H␈↓¬␈↓ βλ    (PROG (?I L)
␈↓ ↓H␈↓¬␈↓ βλ        (SETQ L ??LIST)
␈↓ ↓H␈↓¬␈↓ βλ       LOOP
␈↓ ↓H␈↓¬␈↓ βλ        (COND ((NULL L) (RETURN NIL)))
␈↓ ↓H␈↓¬␈↓ βλ        (SETQ ?I (CAR L))
␈↓ ↓H␈↓¬␈↓ βλ        (SETQ L (CDR L))
␈↓ ↓H␈↓¬␈↓ βλ        *STMTS
␈↓ ↓H␈↓¬␈↓ βλ        (GO LOOP)) )

␈↓ ↓H␈↓A call to this macro such as

␈↓ ↓H␈↓¬␈↓ βλ(FOR X IN '(A B C) DO
␈↓ ↓H␈↓¬␈↓ βλ  (COND ((GET X 'FN) (APPLY (GET X 'FN) NIL)))
␈↓ ↓H␈↓¬␈↓ βλ  (PRINT X))

␈↓ ↓H␈↓would result in the following code to be evaluated

␈↓ ↓H␈↓¬␈↓ βλ(PROG (X L)
␈↓ ↓H␈↓¬␈↓ βλ    (SETQ L '(A B C))
␈↓ ↓H␈↓¬␈↓ βλ   LOOP
␈↓ ↓H␈↓¬␈↓ βλ    (COND ((NULL L) (RETURN NIL)))
␈↓ ↓H␈↓¬␈↓ βλ    (SETQ X (CAR L))
␈↓ ↓H␈↓¬␈↓ βλ    (SETQ L (CDR L))
␈↓ ↓H␈↓␈↓ ¬|Chapter  V␈↓ ≠123


␈↓ ↓H␈↓¬␈↓ βλ    (COND ((GET X 'FN) (APPLY (GET X 'FN) NIL)))
␈↓ ↓H␈↓¬␈↓ βλ    (PRINT X)
␈↓ ↓H␈↓¬␈↓ βλ    (GO LOOP))

␈↓ ↓H␈↓The␈αtask␈αthen␈αis␈αto␈αwrite␈αthe␈αprogram␈α␈↓↓macro-maker␈↓␈αwhich␈αwill␈αput␈αa␈αsuitable␈αmacro␈αdefinition␈αon
␈↓ ↓H␈↓the property list of the indicated atom.

␈↓ ↓H␈↓α␈↓ ∧wStructure modifying functions

␈↓ ↓H␈↓5.␈α∞Write␈α∞a␈α∞function␈α∞that␈α∞reverses␈α∞a␈α∞list␈α∞by␈α∞reversing␈α∞the␈α∞pointers␈α∞and␈α∞thus␈α∞not␈α∞copying␈α∞the␈α∞list.
␈↓ ↓H␈↓How␈α∩could␈α∩you␈α∩prove␈α∩your␈α∩function␈α∩correct?␈α∪ (The␈α∩latter␈α∩is␈α∩a␈α∩non-trivial␈α∩task␈α∩to␈α∪carry␈α∩out
␈↓ ↓H␈↓formally.)

␈↓ ↓H␈↓α␈↓ ¬≤Re-entrant list structures.

␈↓ ↓H␈↓6. Write a program to list the atoms in a possibly re-entrant list structure.

␈↓ ↓H␈↓7.␈α
Write␈α
a␈α
program␈α
to␈αtranslate␈α
a␈α
graph␈α
description␈α
from␈α
the␈αnotation␈α
of␈α
Chapter␈α
I␈α
to␈αa␈α
re-entrant
␈↓ ↓H␈↓list structure.

␈↓ ↓H␈↓8.␈α∂Write␈α∞a␈α∂function␈α∂␈↓↓mk-label␈↓␈α∞which␈α∂takes␈α∞a␈α∂name␈α∂(atom)␈α∞and␈α∂an␈α∞expression␈α∂as␈α∂arguments␈α∞and
␈↓ ↓H␈↓replaces␈α∂all␈α∂occurrences␈α∂of␈α∂the␈α∂name␈α∂by␈α∂a␈α∂pointer␈α∂to␈α∂the␈α∂expression␈α∂itself.␈α∂ When␈α∂applied␈α∂to␈α∞a
␈↓ ↓H␈↓lambda-expression␈α
this␈αcreates␈α
a␈α
self-referential␈αexpression␈α
which␈α
when␈αapplied␈α
to␈αan␈α
appropriate
␈↓ ↓H␈↓list␈α
of␈α
arguments␈α
returns␈α
the␈α
same␈α
value␈α
as␈α
if␈α
␈↓↓label[name,expression]␈↓␈α
had␈α
been␈α
applied␈α
to␈α
the␈α
same
␈↓ ↓H␈↓arguments thus simulating the ␈↓↓label␈↓ construct.

␈↓ ↓H␈↓9.␈αWrite␈αfunctions␈αto␈αtranslate␈αin␈αeach␈αdirection␈αbetween␈αpossibly␈αre-entrant␈αstructures␈αand␈αthe␈αS-
␈↓ ↓H␈↓expressions corresponding to them using the ␈↓¬LABEL, ␈↓␈↓¬POINTER ␈↓notation described above.

␈↓ ↓H␈↓10.␈α
Write␈α
an␈α
axiom␈α
schema␈α
of␈α
induction␈α
for␈α
finite␈α
possibly␈α
re-entrant␈α
list␈α
structures.␈α
 Hint:␈α
The
␈↓ ↓H␈↓schema␈α⊂can␈α∂be␈α⊂based␈α⊂on␈α∂an␈α⊂assertion␈α⊂of␈α∂convergence␈α⊂of␈α∂a␈α⊂program␈α⊂that␈α∂scans␈α⊂a␈α⊂list␈α∂structure
␈↓ ↓H␈↓keeping track of the vertices it has already visited.
␈↓ ↓H␈↓124␈↓ εH␈↓ H


␈↓ ↓H␈↓α␈↓ ¬{Chapter VI

␈↓ ↓H␈↓α␈↓ ¬:HOW LISP WORKS


␈↓ ↓H␈↓        The␈αpurpose␈αof␈αthis␈αchapter␈αis␈αto␈αgive␈αyou␈αan␈αidea␈αof␈αwhat␈αit␈αtakes␈αto␈αmake␈αLISP␈αactually
␈↓ ↓H␈↓run␈αon␈αa␈αcomputer.␈α  It␈αis␈αnot␈αintended␈αas␈αa␈αmanual␈αfor␈αany␈αparticular␈αsystem␈αalthough␈αhopefully
␈↓ ↓H␈↓an␈αunderstanding␈αof␈αthe␈αbasic␈αcomponents␈αof␈αLISP␈αas␈αa␈αinteractive␈αprogramming␈αsystem␈αwill␈α
help
␈↓ ↓H␈↓you␈α⊂understand␈α∂and␈α⊂use␈α∂any␈α⊂particular␈α∂LISP␈α⊂system.␈α∂ We␈α⊂will␈α∂discuss␈α⊂components␈α⊂common␈α∂to
␈↓ ↓H␈↓essentially all LISP systems.  There are some hints as to the variety of possible implementations.

␈↓ ↓H␈↓        The␈α⊂organization␈α⊂of␈α⊂the␈α∂chapter␈α⊂is␈α⊂"bottom␈α⊂up".␈α⊂ We␈α∂begin␈α⊂with␈α⊂as␈α⊂discussion␈α⊂of␈α∂LISP
␈↓ ↓H␈↓objects␈α⊂--␈α∂the␈α⊂basic␈α⊂data␈α∂structures␈α⊂understood␈α⊂and␈α∂manipulated␈α⊂by␈α⊂a␈α∂LISP␈α⊂system.␈α⊂ Next␈α∂we
␈↓ ↓H␈↓describe␈α
the␈αthree␈α
main␈αcomponents␈α
--␈α␈↓↓read,␈↓␈α
␈↓↓eval␈↓␈α
and␈α␈↓↓print␈↓␈α
--␈αwhich␈α
make␈αup␈α
the␈α"Top-level"␈α
of
␈↓ ↓H␈↓LISP.␈α_ This␈α_ "read-eval-print␈α_loop"␈α_is␈α_generally␈α_what␈α_you␈α_talk␈α_to␈α_when␈α→running␈α_LISP
␈↓ ↓H␈↓interactively.␈α  In␈α
principle␈αthis␈αis␈α
all␈αthat␈αis␈α
needed.␈α  However,␈αin␈α
order␈αto␈α
interact␈αproductively
␈↓ ↓H␈↓with␈α∞a␈α
programming␈α∞system␈α
we␈α∞require␈α
of␈α∞it␈α∞more␈α
than␈α∞just␈α
the␈α∞ability␈α
to␈α∞run␈α∞programs.␈α
 Most
␈↓ ↓H␈↓LISP␈α∞systems␈α∞have␈α∞facilities␈α∞for␈α∞making␈α∞output␈α∞more␈α∞readable␈α∞--␈α∞known␈α∞as␈α∞pretty-printing,␈α
for
␈↓ ↓H␈↓editing␈αprograms,␈αfor␈αdetecting␈αand␈αreporting␈αerrors,␈αfor␈αaiding␈αin␈αthe␈αdebugging␈αprocess␈αand␈αfor
␈↓ ↓H␈↓interacting␈α∞with␈α∞the␈α∂host␈α∞system␈α∞--␈α∂external␈α∞file␈α∞manipulation␈α∞for␈α∂example.␈α∞ The␈α∞ability␈α∂to␈α∞run
␈↓ ↓H␈↓programs␈αinterpretively␈αand␈αthe␈αvery␈αsimple␈αsyntax␈α
of␈αLISP␈αmake␈αit␈αpossible␈αto␈αhave␈αvery␈α
elegant
␈↓ ↓H␈↓and␈α∞flexible␈α∂editing,␈α∞error-handling,␈α∞and␈α∂debugging␈α∞facilities␈α∂as␈α∞an␈α∞integral␈α∂part␈α∞of␈α∂the␈α∞system.
␈↓ ↓H␈↓We␈α∃present␈α∃a␈α∀simple␈α∃editor␈α∃based␈α∀on␈α∃the␈α∃MACLISP␈α∀editor␈α∃and␈α∃indicate␈α∃several␈α∀possible
␈↓ ↓H␈↓extensions.␈α∂ We␈α∂discuss␈α∂how␈α∂tracing␈α∂and␈α∂stepping␈α∂through␈α∂the␈α∂execution␈α∂of␈α∂a␈α∂program␈α∂can␈α∞be
␈↓ ↓H␈↓done␈α∞in␈α∞LISP.␈α∞ Finally␈α∞there␈α∞is␈α∞a␈α∞brief␈α∞section␈α∞on␈α∞I/O.␈α∞ This␈α∞aspect␈α∞of␈α∞LISP␈α∞(or␈α∞any␈α∞system)␈α
is
␈↓ ↓H␈↓necessarily␈αimplementation␈αdependent.␈α However␈αthere␈αare␈αsome␈αbasic␈αoperations␈αcommon␈αto␈αmost
␈↓ ↓H␈↓LISPs and we present (an abstraction of)  them.

␈↓ ↓H␈↓[Remark:␈α in␈αthe␈αdescription␈αof␈αthe␈αbasic␈αLISP␈αstructures,␈αwe␈αrefer␈αoccasionaly␈αto␈αprograms␈αwhich
␈↓ ↓H␈↓aren't␈α∞directly␈α∞available␈α∞to␈α∞the␈α∞user.␈α∂ They␈α∞may␈α∞or␈α∞may␈α∞not␈α∞be␈α∞explicitly␈α∂implemented.␈α∞ Mainly,
␈↓ ↓H␈↓they are convenient for purposes of explanation.]



␈↓ ↓H␈↓1.  ␈↓αLISP objects.␈↓


␈↓ ↓H␈↓        A␈αLISP␈αsystem␈αdeals␈αwith␈αa␈αvariety␈αof␈αobjects␈αin␈αthe␈αprocess␈αof␈α
representing␈αS-expressions
␈↓ ↓H␈↓as␈α∪list-structures␈α∪and␈α∪carrying␈α∪out␈α∀computations␈α∪on␈α∪these␈α∪structures.␈α∪ These␈α∀include␈α∪pnames
␈↓ ↓H␈↓(corresponding␈α
to␈α
the␈α
character␈α
string␈α
representation␈α
of␈α
atoms)␈α
numeric␈α
and␈α
symbolic␈α
atoms,␈α
oblists
␈↓ ↓H␈↓(LISPs␈αversion␈αof␈αa␈αsymbol␈αtable)␈αand␈αpointers␈αof␈αvarious␈αflavors.␈α In␈αthis␈αsection␈αwe␈αwill␈αdiscuss
␈↓ ↓H␈↓the construction and basic operations on these objects.


␈↓ ↓H␈↓αPnames.

␈↓ ↓H␈↓        Each␈α⊂atom␈α⊂has␈α⊂a␈α⊂print␈α⊂name␈α⊂(pname␈α⊃for␈α⊂short)␈α⊂which␈α⊂is␈α⊂the␈α⊂link␈α⊂between␈α⊃the␈α⊂internal
␈↓ ↓H␈↓representation␈α⊗of␈α↔atoms␈α⊗and␈α⊗the␈α↔external␈α⊗representation␈α⊗ as␈α↔a␈α⊗character␈α⊗string.␈α↔ The␈α⊗key
␈↓ ↓H␈↓component␈α
of␈α
the␈α
LISP␈αread␈α
program␈α
is␈α
␈↓↓readatom␈↓␈αwhich␈α
converts␈α
the␈α
character␈αstring␈α
representing
␈↓ ↓H␈↓an␈α∞atom␈α∂externally␈α∞to␈α∂the␈α∞corresponding␈α∂pname.␈α∞ The␈α∞pname␈α∂is␈α∞used␈α∂to␈α∞determine␈α∂the␈α∞internal
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠125


␈↓ ↓H␈↓representation␈α∞of␈α∞the␈α∞atom.␈α∞ Correspondingly,␈α∞the␈α∞key␈α∞component␈α∞of␈α∞the␈α∞LISP␈α∞print␈α∂program␈α∞is
␈↓ ↓H␈↓␈↓↓printatom␈↓␈α≥which␈α≤converts␈α≥the␈α≥pname␈α≤to␈α≥the␈α≤corresponding␈α≥external␈α≥(character␈α≤string)
␈↓ ↓H␈↓representation.␈α Clearly␈αif␈αthe␈αmapping␈αbetween␈αrepresentations␈αis␈αto␈αbe␈αcorrect␈αwe␈αmust␈αhave,␈αfor
␈↓ ↓H␈↓atoms␈α␈↓↓a␈↓␈αand␈αcharacter␈αstrings␈α␈↓↓c,␈↓␈α␈↓↓readatom[printatom[a]]=a␈↓␈αand␈α␈↓↓printatom[readatom[c]]␈↓.␈α Generally
␈↓ ↓H␈↓pnames␈α
are␈αnot␈α
directly␈αaccessible␈α
to␈α
the␈αuser.␈α
 They␈αare␈α
however,␈αimportant␈α
properties␈α
of␈αatoms
␈↓ ↓H␈↓and several of the basic operations are conviently described using pnames as intermediate objects.

␈↓ ↓H␈↓[Remark:␈α
␈↓↓readatom␈↓␈α
and␈α
␈↓↓printatom␈↓␈α
are␈α
not␈α
programs␈α
typically␈α
available␈α
directly␈α
to␈α
the␈α
user.␈α
  See
␈↓ ↓H␈↓remark at the end of the introduction to this chapter.]


␈↓ ↓H␈↓αNumeric Atoms.

␈↓ ↓H␈↓        LISP␈αdistinguishes␈α
two␈αkinds␈α
of␈αatoms,␈α
numeric␈αand␈α
symbolic.␈α Numeric␈α
atoms␈αare␈α
generally
␈↓ ↓H␈↓assumed␈αto␈αdenote␈αparticular␈α
numbers␈αand␈αcan␈αbe␈α
recognized,␈αrepresented␈αand␈αprinted␈α
without␈αa
␈↓ ↓H␈↓lot␈αof␈αadditional␈αinformation.␈α Thus,␈αalthough␈αnumbers␈αare␈αclassed␈αas␈αatoms␈α(e.g.␈α␈↓αat|␈↓1␈αis␈αtrue)␈αthe
␈↓ ↓H␈↓representation␈αof␈αnumbers␈αin␈α
LISP␈αis␈αusually␈αdifferent␈αthan␈α
that␈αof␈αsymbolic␈αatoms.␈α Numbers␈α
are
␈↓ ↓H␈↓not␈α∞entered␈α∞on␈α∂the␈α∞oblist␈α∞and␈α∞they␈α∂do␈α∞not␈α∞have␈α∞property␈α∂lists.␈α∞ Their␈α∞pnames␈α∞and␈α∂"values"␈α∞are
␈↓ ↓H␈↓computed␈α∂from␈α∂the␈α∂representation␈α∂and␈α∂cannot␈α∂be␈α∞accessed␈α∂in␈α∂the␈α∂usual␈α∂way.␈α∂In␈α∂particular,␈α∞you
␈↓ ↓H␈↓cannot␈α⊂set␈α⊂the␈α⊃"value"␈α⊂of␈α⊂a␈α⊃numeric␈α⊂atom␈α⊂(which␈α⊂is␈α⊃probably␈α⊂just␈α⊂as␈α⊃well)␈α⊂so␈α⊂they␈α⊃really␈α⊂are
␈↓ ↓H␈↓constants.␈α One␈αpossible␈αrepresentation␈αof␈αnumbers␈αis␈αby␈αa␈αcell␈αthe␈αa-part␈αof␈αwhich␈αsays␈αthis␈αis␈αan
␈↓ ↓H␈↓atom,␈α∪and␈α∪d-part␈α∪which␈α∪is␈α∪a␈α∪pair␈α∪consisting␈α∪of␈α∪the␈α∪type␈α∪and␈α∪a␈α∪pointer␈α∪to␈α∪actual␈α∪machine
␈↓ ↓H␈↓representation␈α∀of␈α∀the␈α∀numerical␈α∀value.␈α∃ Sufficiently␈α∀small␈α∀integers␈α∀can␈α∀be␈α∃represented␈α∀more
␈↓ ↓H␈↓compactly␈α
by␈α
a␈α∞cell␈α
the␈α
a-part␈α
of␈α∞which␈α
is␈α
a␈α
special␈α∞flag␈α
and␈α
the␈α
d-part␈α∞of␈α
which␈α
is␈α∞a␈α
machine
␈↓ ↓H␈↓representation␈αof␈αthe␈αnumerical␈αvalue.␈α Another␈αpossibility␈αis␈αto␈αstore␈αall␈αnumbers␈αof␈α
a␈αparticular
␈↓ ↓H␈↓kind␈α∃in␈α∃a␈α∃particular␈α∃area␈α∃and␈α∃represent␈α∃a␈α∃number␈α∃by␈α∃a␈α∃pointer␈α∃directly␈α∃to␈α∃the␈α∀machine
␈↓ ↓H␈↓representation␈α⊃of␈α⊃its␈α⊃value.␈α⊃ As␈α⊃we␈α∩mentioned␈α⊃in␈α⊃Chapter␈α⊃I,␈α⊃LISP␈α⊃can␈α⊃treat␈α∩arbitrarily␈α⊃large
␈↓ ↓H␈↓integers␈αby␈αrepresenting␈αthem␈αas␈αa␈αlist␈α"digits"␈αwith␈αrespect␈αto␈αsome␈αlarge␈αbase.␈α Typically␈αthe␈αfirst
␈↓ ↓H␈↓element␈αof␈αthe␈αlist␈αis␈αa␈α
flag␈αsaying␈α"I␈αam␈αa␈αbignum"␈αand␈α
a␈αsign.␈α In␈αany␈αcase␈αthe␈αreading,␈α
printing,
␈↓ ↓H␈↓and␈α
other␈α
primitive␈αprograms␈α
of␈α
LISP␈α
must␈αpay␈α
attention␈α
to␈α
whether␈αor␈α
not␈α
the␈α
object␈αbeing␈α
dealt
␈↓ ↓H␈↓with is a symbolic atom or a numerical atom or some non-atomic object.


␈↓ ↓H␈↓αSymbolic Atoms: property lists

␈↓ ↓H␈↓        Symbolic␈αatoms␈αdo␈αnot␈α
in␈αgeneral␈αhave␈αa␈α
meaning␈αfixed␈αby␈αthe␈α
system.␈α They␈αcan␈αbe␈α
names
␈↓ ↓H␈↓of␈αvariables,␈αprograms,␈αmacros,␈αor␈αarrays␈αand␈αas␈αsuch␈αmust␈αbe␈αcapable␈αof␈αhaving␈αvalues,␈αprogram
␈↓ ↓H␈↓code␈α⊃(compiled␈α∩or␈α⊃in␈α∩S-expression␈α⊃form),␈α∩macro␈α⊃definitions,␈α∩and␈α⊃array␈α∩descriptions␈α⊃(pointers)
␈↓ ↓H␈↓associated␈α∂with␈α∂them.␈α∂ In␈α∂addition␈α∂arbitrary␈α⊂user␈α∂defined␈α∂properties␈α∂can␈α∂be␈α∂associated␈α⊂with␈α∂an
␈↓ ↓H␈↓atom.␈α In␈αthe␈αsimplest␈αcase␈αLISP␈αtreats␈α
all␈αproperties␈αof␈αan␈αatom␈α(including␈αits␈α
pname)␈αuniformly
␈↓ ↓H␈↓by␈α⊂putting␈α⊂them␈α⊂on␈α⊂a␈α⊂property␈α∂list.␈α⊂ In␈α⊂this␈α⊂case␈α⊂the␈α⊂atom␈α∂is␈α⊂represented␈α⊂by␈α⊂a␈α⊂pointer␈α⊂to␈α∂the
␈↓ ↓H␈↓property␈α∩list␈α∪and␈α∩all␈α∪properties␈α∩can␈α∪be␈α∩accessed␈α∩and␈α∪set␈α∩uniformly␈α∪by␈α∩special␈α∪operations␈α∩on
␈↓ ↓H␈↓property␈α
lists.␈α
 In␈α
order␈α
to␈αdistuingish␈α
an␈α
atom␈α
(e.g.␈α
a␈αproperty␈α
list)␈α
from␈α
other␈α
list-structure,␈αthe␈α
a-
␈↓ ↓H␈↓part␈α⊃of␈α⊂the␈α⊃first␈α⊃element␈α⊂contains␈α⊃data␈α⊃that␈α⊂is␈α⊃recognized␈α⊃as␈α⊂an␈α⊃"I␈α⊃am␈α⊂an␈α⊃atom"␈α⊃flag.␈α⊂  This
␈↓ ↓H␈↓uniformness␈α
is␈α
simple␈α∞and␈α
elegant.␈α
 Information␈α
stored␈α∞on␈α
a␈α
property␈α
list␈α∞can␈α
be␈α
retrieved␈α∞for␈α
a
␈↓ ↓H␈↓particular␈αatom␈αin␈αa␈αtime␈αthat␈αdepends␈αon␈αthe␈αlength␈αof␈αthe␈αproperty␈αlist␈αof␈αthe␈αatom,␈αand␈αthis␈αis
␈↓ ↓H␈↓usually␈α∞quite␈α
short.␈α∞ The␈α∞time␈α
is␈α∞independent␈α
of␈α∞the␈α∞number␈α
of␈α∞atoms.␈α
 There␈α∞is␈α∞some␈α
inherent
␈↓ ↓H␈↓inefficiency␈α⊂when␈α⊂frequently␈α⊂used␈α⊂properties,␈α⊂like␈α∂value,␈α⊂are␈α⊂not␈α⊂accessed␈α⊂directly␈α⊂but␈α⊂must␈α∂be
␈↓ ↓H␈↓looked␈αup␈αlike␈αuser␈αdefined␈αproperties.␈α Also␈αthe␈αuniformity␈αallows␈αthe␈αprogrammer␈αto␈α
manipulate
␈↓ ↓H␈↓the␈α∩state␈α∩of␈α∩the␈α∩system␈α∪in␈α∩arbitrarily␈α∩complicated␈α∩and␈α∩unmanagable␈α∩ways.␈α∪ Another␈α∩possible
␈↓ ↓H␈↓126␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓representation␈α
of␈α
an␈α
atom␈α
is␈α
a␈α
pointer␈α
to␈αa␈α
block␈α
of␈α
memory␈α
with␈α
system␈α
properties␈α
such␈αas␈α
pname,
␈↓ ↓H␈↓value,␈α(possibly)␈αprogram␈αdescriptions,␈αand␈αa␈αpointer␈αto␈αthe␈αlist␈αfor␈αuser␈αdefined␈αproperties␈αstored
␈↓ ↓H␈↓in␈α
fixed␈α
locations␈α
relative␈α
the␈α
block␈α
pointer.␈α
 There␈α
may␈α
also␈α
be␈α
restricted␈α
access␈α
to␈α
some␈α∞of␈α
the
␈↓ ↓H␈↓system properties, thus imposing a small amount of discipline on the programmer.

␈↓ ↓H␈↓        Property␈α⊂lists␈α∂are␈α⊂usually␈α⊂realized␈α∂as␈α⊂ordinary␈α⊂lists.␈α∂ Each␈α⊂"property"␈α⊂has␈α∂an␈α⊂atom␈α⊂as␈α∂its
␈↓ ↓H␈↓name,␈α∂and␈α∂the␈α∞name␈α∂of␈α∂the␈α∞property␈α∂is␈α∂ordinarily␈α∞followed␈α∂by␈α∂a␈α∞pointer␈α∂to␈α∂the␈α∂information␈α∞in
␈↓ ↓H␈↓question.␈α∞ Because␈α∞this␈α∞information␈α∞can␈α
be␈α∞a␈α∞string␈α∞of␈α∞characters,␈α
a␈α∞floating␈α∞point␈α∞number␈α∞or␈α
a
␈↓ ↓H␈↓pointer␈αto␈α
a␈αsubroutine,␈αthe␈α
property␈αlist␈α
is␈αordinarily␈αnot␈α
a␈αproper␈αLISP␈α
list,␈αand␈α
programs␈αthat
␈↓ ↓H␈↓operate␈αon␈αproper␈αlists␈αcan␈αcause␈αerrors␈αif␈αapplied␈αto␈αproperty␈αlists.␈α For␈αexample,␈αa␈αprogram␈αthat
␈↓ ↓H␈↓interpreted␈α⊃a␈α∩floating␈α⊃point␈α∩number␈α⊃as␈α∩a␈α⊃pair␈α⊃of␈α∩pointers␈α⊃would␈α∩interpret␈α⊃random␈α∩places␈α⊃in
␈↓ ↓H␈↓memory as list structure.

␈↓ ↓H␈↓        For␈αthis␈αreason,␈αproperty␈α
lists␈αare␈αusually␈αread␈αand␈α
modified␈αby␈αspecial␈αprograms.␈α We␈α
have
␈↓ ↓H␈↓already␈αseen␈α
two␈αsuch␈α
programs,␈αnamely␈α␈↓¬DEFUN␈α
␈↓and␈α␈↓¬DEFPROP␈α
␈↓which␈αcan␈αbe␈α
used␈αto␈α
put␈αfunction
␈↓ ↓H␈↓definitions on property lists.  The three basic programs on property lists are:

␈↓ ↓H␈↓␈↓ ∧8(␈↓¬GET ␈↓<atom> <property>)
␈↓ ↓H␈↓␈↓ ∧8(␈↓¬PUTPROP ␈↓<atom> <value> <property>)
␈↓ ↓H␈↓␈↓ ∧8(␈↓¬REMPROP ␈↓<atom> <property>)

␈↓ ↓H␈↓where␈α<atom>␈αshould␈αevaluate␈αto␈αan␈αatom,␈α<property>␈αmust␈αevaluate␈αto␈αan␈αatom␈αwhich␈αnames␈α
the
␈↓ ↓H␈↓property,␈α∂and␈α⊂<value>␈α∂can␈α⊂be␈α∂any␈α∂LISP␈α⊂expression␈α∂that␈α⊂can␈α∂be␈α∂evaluated.␈α⊂ [In␈α∂some␈α⊂LISPs␈α∂a
␈↓ ↓H␈↓property␈α↔list␈α↔is␈α↔also␈α↔an␈α↔accepted␈α↔value␈α↔for␈α↔<atom>.]␈α↔The␈α↔exact␈α↔form␈α↔of␈α↔these␈α↔terms␈α↔is
␈↓ ↓H␈↓implementation dependent, in particular the order in which the arguments are expected may vary.

␈↓ ↓H␈↓        ␈↓¬GET␈α∃␈↓returns␈α∃the␈α∃<property>␈α∃value␈α∃(if␈α∀any)␈α∃associated␈α∃with␈α∃<atom>.␈α∃ ␈↓¬PUTPROP␈α∃␈↓puts␈α∀a
␈↓ ↓H␈↓property␈αname␈α<property>␈αfollowed␈αby␈αthe␈αproperty␈αvalue␈α<value>␈αon␈αthe␈αproperty␈αlist␈αof␈α<atom>,
␈↓ ↓H␈↓and␈α∞␈↓¬REMPROP␈α∞␈↓removes␈α∞a␈α∞property.␈α∞ ␈↓¬DEFPROP␈α∂␈↓is␈α∞like␈α∞␈↓¬PUTPROP␈α∞␈↓except␈α∞that␈α∞the␈α∞arguments␈α∂are␈α∞not
␈↓ ↓H␈↓evaluated.␈α⊂ Thus␈α⊂if␈α⊂␈↓¬(PUTPROP 'C 12 'AT-WT)␈↓␈α⊂is␈α⊂executed␈α⊂then␈α⊂␈↓¬(GET 'C 'AT-WT)␈↓␈α⊃will␈α⊂return
␈↓ ↓H␈↓␈↓¬12.␈α␈↓␈αIf␈α␈↓¬(REMPROP 'C 'AT-WT)␈↓␈αis␈αexecuted␈α
then␈α␈↓¬(GET 'C 'AT-WT)␈↓␈αwill␈αreturn␈α␈↓¬NIL␈↓␈αas␈α
the␈α␈↓¬AT-WT
␈↓ ↓H␈↓¬␈↓property␈α∂is␈α∂no␈α∂longer␈α∂on␈α∂the␈α∂property␈α∂list␈α∂of␈α∂␈↓¬C.␈α∂␈↓␈α∂If␈α∂␈↓¬(DEFPROP APPLE RED COLOR)␈↓␈α∂is␈α∂executed
␈↓ ↓H␈↓then␈α∩␈↓¬(GET 'APPLE 'COLOR)␈↓␈α∩will␈α∩return␈α∩␈↓¬RED.␈α∩␈↓␈α∩Thus␈α∩␈↓¬DEFPROP␈α∩␈↓can␈α∩be␈α∩used␈α∩to␈α∩put␈α∩arbitrary
␈↓ ↓H␈↓properties␈α⊃on␈α⊃property␈α∩lists.␈α⊃ The␈α⊃difference␈α⊃between␈α∩␈↓¬DEFPROP␈α⊃and␈α⊃␈↓␈↓¬PUTPROP␈α⊃␈↓is␈α∩that␈α⊃␈↓¬DEFPROP
␈↓ ↓H␈↓¬␈↓does not evaluate its arguments, but ␈↓¬PUTPROP ␈↓does.

␈↓ ↓H␈↓        Most␈αLISPs␈α
allow␈αyou␈α
to␈αexamine␈α
the␈αproperty␈αlist␈α
of␈αan␈α
atom␈αdirectly.␈α
 In␈αthe␈α
case␈αwhere
␈↓ ↓H␈↓an␈αatom␈αis␈α
represented␈αby␈αa␈αpointer␈α
to␈αits␈αproperty␈αlist,␈α
the␈α␈↓↓car␈↓␈αis␈αand␈α
"atom-header␈αand␈αthe␈α␈↓↓cdr␈↓␈α
is
␈↓ ↓H␈↓a␈α∀list␈α∀of␈α∀alternating␈α∀property␈α∀names␈α∀and␈α∪property␈α∀values.␈α∀  In␈α∀the␈α∀case␈α∀where␈α∀an␈α∀atom␈α∪is
␈↓ ↓H␈↓represented␈αby␈αa␈αpointer␈α
to␈αa␈αblock␈αof␈αmemory␈α
a␈αspecial␈αoperation␈αis␈α
needed␈αto␈αget␈αat␈αthe␈α
property
␈↓ ↓H␈↓list.  For example in MACLISP there ␈↓¬(PLIST 'A)␈↓ is the property list of the atom ␈↓¬A. ␈↓

␈↓ ↓H␈↓        From␈α∀a␈α∃mathematical␈α∀point␈α∃of␈α∀view␈α∀␈↓¬PUTPROP␈α∃␈↓acts␈α∀like␈α∃an␈α∀assignment␈α∃statement␈α∀and
␈↓ ↓H␈↓␈↓¬REMPROP␈α∩␈↓like␈α∩a␈α∩special␈α∩kind␈α∩of␈α∩an␈α∩assignment␈α∩statement.␈α∩ ␈↓¬GET␈α∩␈↓gets␈α∩the␈α∩value␈α∩of␈α∩a␈α⊃variable.
␈↓ ↓H␈↓However,␈α∀the␈α∀mathematical␈α∀properties␈α∀of␈α∀these␈α∀LISP␈α∀operations␈α∀haven't␈α∃been␈α∀systematically
␈↓ ↓H␈↓studied.

␈↓ ↓H␈↓        The␈α∂particular␈α∂collection␈α∂of␈α∂properties␈α∂used␈α∂by␈α∂LISP␈α∂varies,␈α∂however,␈α∂it␈α⊂usually␈α∂includes
␈↓ ↓H␈↓␈↓¬VALUE␈α∪␈↓and␈α∪␈↓¬PNAME␈α∩␈↓as␈α∪well␈α∪as␈α∩properties␈α∪whose␈α∪values␈α∩describe␈α∪"applicative"␈α∪objects␈α∪such␈α∩as
␈↓ ↓H␈↓programs,␈α⊂macros␈α⊃and␈α⊂arrays.␈α⊂ Names␈α⊃for␈α⊂the␈α⊂latter␈α⊃properties␈α⊂inclued␈α⊂␈↓¬EXPR,␈α⊃␈↓␈↓¬FEXPR,␈α⊂␈↓␈↓¬LEXPR,
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠127


␈↓ ↓H␈↓¬␈↓␈↓¬SUBR,␈α∩␈↓␈↓¬FSUBR,␈α∩␈↓␈↓¬LSUBR,␈α⊃␈↓␈↓¬MACRO␈α∩␈↓and␈α∩␈↓¬ARRAY.␈α∩␈↓␈α⊃We␈α∩will␈α∩indicate␈α⊃how␈α∩these␈α∩properties␈α∩are␈α⊃used
␈↓ ↓H␈↓when we discuss the ␈↓↓eval␈↓ component of LISPs Top-level.

␈↓ ↓H␈↓        One␈α
use␈α
of␈αproperty␈α
lists␈α
in␈α
programming␈αis␈α
in␈α
the␈α
creation␈αand␈α
modification␈α
of␈αnetworks␈α
of
␈↓ ↓H␈↓data.␈α∪ Values␈α∩attached␈α∪to␈α∩atoms␈α∪point␈α∩to␈α∪expressions␈α∩that␈α∪include␈α∩other␈α∪atoms.␈α∪ A␈α∩program
␈↓ ↓H␈↓manipulating␈αchemical␈αstructures␈αmight␈α
use␈αproperty␈αlists␈αto␈α
associate␈αsuch␈αinformation␈αas␈α
valence,
␈↓ ↓H␈↓atomic␈α
weight,␈α
nuclear␈αspin,␈α
etc.␈α
 with␈α
each␈αchemical␈α
atom␈α
of␈α
interest.␈α It␈α
could␈α
also␈αtreat␈α
fragments
␈↓ ↓H␈↓as␈αquasi␈αatoms␈α(chemical␈αatoms,␈αthat␈αis)␈αby␈α
naming␈αthem,␈αputting␈αthe␈αstructure␈αof␈αthe␈αfragment␈α
on
␈↓ ↓H␈↓the␈α∞property␈α
list,␈α∞as␈α∞well␈α
as␈α∞analogues␈α
to␈α∞other␈α∞atomic␈α
properties.␈α∞ These␈α
properties␈α∞can␈α∞then␈α
be
␈↓ ↓H␈↓used␈αas␈α
required␈αby␈αprograms␈α
for␈αbuilding␈α
or␈αanalysing␈αcomplex␈α
molecules.␈α Another␈α
example␈αis
␈↓ ↓H␈↓the␈α∞program␈α∂used␈α∞to␈α∂print␈α∞the␈α∞LISP␈α∂programs␈α∞appearing␈α∂in␈α∞this␈α∞book␈α∂in␈α∞external␈α∂form.␈α∞ Each
␈↓ ↓H␈↓special␈αLISP␈αatom␈α(such␈αas␈α␈↓¬CAR,␈α␈↓␈↓¬COND,␈α␈↓␈↓¬APPEND,␈α
␈↓␈↓¬QUOTE,␈α␈↓...)␈αhas␈αon␈αits␈αproperty␈αlist␈αthe␈α
name␈αof
␈↓ ↓H␈↓the␈α
program␈α
used␈α
to␈α
compile␈α
the␈α
construct␈α
it␈α
signals,␈α
type␈α
information,␈α
external␈α
print␈α
name␈α(so␈α
that
␈↓ ↓H␈↓␈↓¬CAR␈α␈↓prints␈αas␈α␈↓αa␈↓␈αfor␈αexample)␈αand␈αother␈αcontrol␈αinformation.␈α This␈αis␈αan␈αexample␈αof␈α"data␈αdriven"
␈↓ ↓H␈↓programming.␈α
 Another␈α
example␈α
of␈α
a␈α
data␈α
driven␈αprogram␈α
would␈α
be␈α
a␈α
compiler␈α
that␈α
looked␈αup
␈↓ ↓H␈↓the␈α⊂program␈α∂to␈α⊂compile␈α∂special␈α⊂constructs␈α∂on␈α⊂the␈α∂␈↓¬COMPFN␈α⊂␈↓property␈α∂of␈α⊂the␈α∂atom␈α⊂signalling␈α∂that
␈↓ ↓H␈↓construct.␈α∪ Such␈α∪a␈α∪compiler␈α∪is␈α∪quite␈α∪flexible␈α∪and␈α∪allows␈α∪you␈α∪to␈α∪extend␈α∪the␈α∪language␈α∪easily.
␈↓ ↓H␈↓Another example of this style of program is the ␈↓↓editor␈↓ given in Chapter VI.


␈↓ ↓H␈↓αSymbolic atoms: Oblists.

␈↓ ↓H␈↓        LISP␈α⊂keeps␈α⊂track␈α⊃of␈α⊂atoms␈α⊂explicitly␈α⊃known␈α⊂by␈α⊂name␈α⊂by␈α⊃making␈α⊂entries␈α⊂in␈α⊃the␈α⊂"oblist"
␈↓ ↓H␈↓(called␈αobarray␈αin␈αsome␈αimplementations).␈α The␈αoblist␈αis␈αused␈αmainly␈αby␈αthe␈αread␈αprogram␈αto␈αlook
␈↓ ↓H␈↓up␈αatoms␈αhaving␈αa␈αgiven␈αpname.␈α If␈αan␈αatom␈αis␈αknown␈α(has␈αbeen␈αmentioned␈αby␈αname␈αpreviously
␈↓ ↓H␈↓and␈αnot␈αexplicitly␈αforgotten)␈αthen␈αa␈αpointer␈αto␈αthe␈αatom␈αcan␈αbe␈αobtained␈αby␈αlooking␈αup␈αthe␈αoblist
␈↓ ↓H␈↓entry␈α∞corresponding␈α∞to␈α
its␈α∞pname.␈α∞ Otherwise␈α∞an␈α
atom␈α∞having␈α∞that␈α
pname␈α∞must␈α∞be␈α∞created␈α
and
␈↓ ↓H␈↓entered␈α⊂in␈α∂the␈α⊂oblist.␈α∂  This␈α⊂protocol␈α∂insures␈α⊂that␈α∂separate␈α⊂mention␈α∂of␈α⊂atoms␈α∂having␈α⊂the␈α∂same
␈↓ ↓H␈↓external representation are represented by the same internal structure.

␈↓ ↓H␈↓        The␈αoblist␈αcould␈αbe␈αan␈αordinary␈αlist␈αmanipulated␈αby␈α␈↓↓car,␈↓␈α␈↓↓cdr,␈↓␈α␈↓↓cons␈↓␈αand␈αeven␈α␈↓↓rplac␈↓'s.␈α Some
␈↓ ↓H␈↓implementations␈αuse␈αarrays␈αrather␈αthan␈αlists␈αto␈αstore␈αthe␈αoblist,␈αhence␈αthe␈αname␈α
"obarray".␈α Often
␈↓ ↓H␈↓the␈α
oblist␈α
is␈α
"hashed"␈α
in␈α
order␈α
to␈α
speed␈α
up␈α
the␈α
search␈α
for␈α
a␈α
particular␈α
entry.␈α
 The␈α
idea␈α
of␈αa␈α
hashed
␈↓ ↓H␈↓list␈α(or␈αarray)␈αis␈αthat␈αit␈αis␈αdivided␈αup␈αinto␈αa␈αfixed␈αnumber␈α␈↓↓n␈↓␈αof␈αsublists␈αcalled␈α"hash␈αbuckets",␈αand
␈↓ ↓H␈↓there␈αis␈αa␈αprogram␈α␈↓↓hash␈↓␈αwhich␈αcomputes␈αa␈αnumber␈αbetween␈α1␈αand␈α␈↓↓n␈↓␈αfor␈αeach␈αpossible␈αelement␈αof
␈↓ ↓H␈↓the␈α∂list.␈α∂ If␈α⊂␈↓↓hash[e]␈↓␈α∂is␈α∂␈↓↓k␈↓␈α⊂then␈α∂␈↓↓e␈↓␈α∂is␈α∂in␈α⊂the␈α∂␈↓↓k␈↓th␈α∂bucket␈α⊂and␈α∂the␈α∂search␈α∂for␈α⊂␈↓↓e␈↓␈α∂can␈α∂be␈α⊂restricted␈α∂to
␈↓ ↓H␈↓searching␈αthat␈αbucket␈αrather␈αthan␈αthe␈αwhole␈αlist.␈α In␈αthe␈αcase␈αof␈αoblists␈αthe␈αargument␈αto␈α␈↓↓hash␈↓␈αis␈αa
␈↓ ↓H␈↓pname.␈α  If␈α
the␈αhashed␈α
list␈αis␈α
to␈αbe␈α
an␈αimprovement␈αover␈α
a␈αsimple␈α
linear␈αlist␈α
as␈αfar␈α
as␈αsearching
␈↓ ↓H␈↓efficiency␈αis␈αconcerned,␈αthe␈αhash␈αprogram␈αshould␈αdistribute␈αthe␈αelements␈αof␈αthe␈αlist␈αevenly␈αamong
␈↓ ↓H␈↓the␈α∂buckets.␈α∂ This␈α∂is␈α∂not␈α∂always␈α∂easy␈α∂to␈α∂do.␈α∂ For␈α∂a␈α∂further␈α∂discussion␈α∂of␈α∂hashing␈α∂as␈α∂a␈α∞general
␈↓ ↓H␈↓searching method see Knuth[1968b].

␈↓ ↓H␈↓        There␈αare␈αtwo␈αbasic␈α
operations␈αfor␈αmodifying␈αan␈αoblist,␈α
␈↓↓intern␈↓␈αand␈α␈↓↓remob.␈↓␈α ␈↓↓intern␈↓␈α
takes␈αan
␈↓ ↓H␈↓atomic␈αobject␈α(a␈αpointer␈αto␈αit)␈αand␈αreturns␈αa␈αpointer␈αto␈αan␈αatom␈αwith␈αthe␈αsame␈αpname␈αwhich␈αis␈αa
␈↓ ↓H␈↓member␈αof␈αthe␈α
oblist.␈α If␈αno␈α
atom␈αwith␈αthe␈α
same␈αpname␈αis␈α
on␈αthe␈αoblist␈α
then␈α␈↓↓intern␈↓␈αputs␈αthe␈α
given
␈↓ ↓H␈↓atom␈αthere,␈αand␈αthe␈αreturned␈αpointer␈α will␈αthus␈αbe␈αequal␈αto␈αthe␈αinput␈αpointer.␈α  If␈αan␈αatom␈αof␈αthe
␈↓ ↓H␈↓same␈αname␈αis␈αalready␈αon␈αthe␈α oblist,␈αthen␈αthe␈αpointer␈αto␈αthat␈αatom␈αis␈αreturned␈αand␈αit␈αmay␈αor␈αmay
␈↓ ↓H␈↓not␈αbe␈αequal␈αto␈αthe␈αinput␈αpointer.␈α (A␈αcleaner␈αexplanation␈αof␈α␈↓↓intern␈↓␈αwould␈αrequire␈αan␈αoblist␈αas␈αan
␈↓ ↓H␈↓argument␈α⊂also.␈α∂ In␈α⊂LISP␈α∂many␈α⊂operations␈α∂use␈α⊂some␈α∂part␈α⊂of␈α∂the␈α⊂LISP␈α∂environment␈α⊂as␈α∂implicit
␈↓ ↓H␈↓arguments.␈α Of␈αcourse␈αthey␈αalso␈αmay␈αupdate␈αthe␈αenvironment␈αwhen␈αexecuted.)  ␈α␈↓↓remob␈↓␈αis␈αthe␈αatom
␈↓ ↓H␈↓128␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓forgetting␈α∞program.␈α∞ It␈α∞takes␈α∂an␈α∞atomic␈α∞object␈α∞as␈α∞an␈α∂argument␈α∞and␈α∞removes␈α∞it␈α∞from␈α∂ the␈α∞oblist.
␈↓ ↓H␈↓␈↓↓remob␈↓␈αdoesn't␈αdestroy␈αthe␈αatom,␈αit␈αjust␈αcan␈α
no␈αlonger␈αbe␈αreferenced␈αby␈αname.␈α ␈↓↓remob␈↓␈αand␈α␈↓↓intern␈↓␈α
are
␈↓ ↓H␈↓usually␈αavailable␈αdirectly␈αto␈αthe␈αuser.␈α As␈αwith␈αany␈αdestructive␈αoperation␈αthe␈αuse␈αof␈α␈↓↓remob␈↓␈αcan␈αget
␈↓ ↓H␈↓you␈αinto␈αtrouble␈αif␈αyou␈αare␈αnot␈αcareful,␈αand␈αare␈αnot␈αquite␈αsure␈αof␈αthe␈αstate␈αof␈αthe␈αworld␈αwhen␈αyou
␈↓ ↓H␈↓do it.

␈↓ ↓H␈↓        The␈α
basic␈α
primitive␈αfor␈α
creating␈α
atoms␈α
is␈α␈↓↓mkatom␈↓␈α
which␈α
takes␈α
a␈αpname␈α
and␈α
returns␈αan␈α
atom
␈↓ ↓H␈↓with␈α
that␈α
pname␈α∞and␈α
with␈α
no␈α
other␈α∞properties␈α
set.␈α
 ␈↓↓mkatom␈↓␈α
is␈α∞not␈α
directly␈α
available␈α
to␈α∞the␈α
user,
␈↓ ↓H␈↓but is used (possibly implicitly) by other system programs that manipulate atoms.


␈↓ ↓H␈↓αMaking atoms: Examples.

␈↓ ↓H␈↓        LISP␈α∞makes␈α
available␈α∞to␈α
the␈α∞programmer␈α
several␈α∞operations␈α
on␈α∞atoms␈α
and␈α∞oblists.␈α
 These
␈↓ ↓H␈↓include␈α
␈↓↓maknam,␈↓␈α
␈↓↓implode␈↓␈αand␈α
␈↓↓gensym␈↓␈α
for␈αmaking␈α
atoms,␈α
␈↓↓explode␈↓␈αfor␈α
analysing␈α
pnames␈αand␈α
␈↓↓intern␈↓
␈↓ ↓H␈↓and␈α⊂␈↓↓remob␈↓␈α⊂for␈α∂updating␈α⊂the␈α⊂oblist.␈α∂ The␈α⊂oblist␈α⊂operations␈α∂were␈α⊂described␈α⊂above.␈α⊂ (You␈α∂should
␈↓ ↓H␈↓consult␈α∀the␈α∀manual␈α∀for␈α∀the␈α∀LISP␈α∀you␈α∀are␈α∀using,␈α∀for␈α∀details␈α∀about␈α∀the␈α∀precise␈α∀names␈α∪and
␈↓ ↓H␈↓conventions␈α
for␈α
using␈α
these␈α
programs.)␈α
In␈α
the␈α
following␈α
we␈α
attempt␈α
to␈α
explain␈α
the␈α
subtilties␈α
and
␈↓ ↓H␈↓uses of the programs for making atoms.

␈↓ ↓H␈↓        First␈α
we␈α
consider␈αwhy␈α
the␈α
ability␈αto␈α
make␈α
atoms␈α
(other␈αthan␈α
as␈α
needed␈αwhen␈α
the␈α
are␈αread)␈α
is
␈↓ ↓H␈↓useful.␈α In␈αsome␈αsense␈αthe␈αusefulness␈αof␈αthis␈αand␈αother␈αfeatures␈αis␈αlimited␈αonly␈αby␈αthe␈αimagination
␈↓ ↓H␈↓of␈α
the␈α
programmer.␈α
 One␈α
class␈α
of␈α
programs␈α
that␈α
use␈α
the␈α
atom␈α
making␈α
ability␈α
are␈α
programs␈α
that
␈↓ ↓H␈↓need␈α⊂to␈α⊃invent␈α⊂labels␈α⊂for␈α⊃some␈α⊂reason.␈α⊃ Compilers␈α⊂are␈α⊂in␈α⊃this␈α⊂class.␈α⊂ The␈α⊃typical␈α⊂output␈α⊃of␈α⊂a
␈↓ ↓H␈↓compiler␈α⊃is␈α⊃a␈α⊃list␈α⊃of␈α⊃symbolic␈α⊃assembly␈α⊂instructions␈α⊃some␈α⊃of␈α⊃which␈α⊃are␈α⊃labeled␈α⊃with␈α⊂symbolic
␈↓ ↓H␈↓addresses␈α
which␈α
are␈αreferenced␈α
in␈α
other␈αinstructions.␈α
 Other␈α
members␈αof␈α
the␈α
class␈αinclude␈α
program
␈↓ ↓H␈↓generating␈α∩and␈α∩program␈α∩transforming␈α∩programs␈α∩that␈α∩need␈α∩to␈α∩introduce␈α∩new␈α∪variable␈α∩names,
␈↓ ↓H␈↓statement␈αlabels,␈αetc..␈α Another␈αexample␈αis␈αa␈αprogram␈αthat␈αuses␈αtemporary␈αfiles␈αas␈αwork␈αspace␈αand
␈↓ ↓H␈↓the␈α∞number␈α∞of␈α∞files␈α∞needed␈α∞is␈α∞not␈α∞known␈α∞in␈α∞advance.␈α∞ Therefore␈α∞it␈α∞is␈α∞convenient␈α∞to␈α∞be␈α∂able␈α∞to
␈↓ ↓H␈↓generate␈α∂file␈α∂names.␈α∂ Finally␈α⊂in␈α∂a␈α∂system␈α∂of␈α∂programs␈α⊂for␈α∂manipulating␈α∂logical␈α∂formulae␈α⊂or␈α∂λ-
␈↓ ↓H␈↓expressions␈α∀a␈α∃program␈α∀that␈α∀substitutes␈α∃an␈α∀expression␈α∀for␈α∃occurrences␈α∀of␈α∀a␈α∃bound␈α∀variable
␈↓ ↓H␈↓generally␈α∞needs␈α∞to␈α∂be␈α∞able␈α∞to␈α∞rename␈α∂bound␈α∞variables␈α∞in␈α∞order␈α∂to␈α∞avoid␈α∞conflict.␈α∞ This␈α∂is␈α∞most
␈↓ ↓H␈↓conveniently done by providing a means of generating new variable names as the need arises.

␈↓ ↓H␈↓        The␈αoperation␈α␈↓↓maknam␈↓␈αtakes␈αa␈αlist␈αof␈αcharacters␈α(atoms␈αwhose␈αpname␈αis␈αa␈αsingle␈αcharacter)
␈↓ ↓H␈↓and␈α
returns␈α
an␈α
atom␈αwhose␈α
pname␈α
is␈α
the␈αconcatenation␈α
of␈α
this␈α
list␈αof␈α
characters.␈α
  It␈α
does␈αnot␈α
enter
␈↓ ↓H␈↓the␈α
atom␈α
on␈α
the␈αoblist,␈α
or␈α
look␈α
to␈αsee␈α
if␈α
an␈α
atom␈αof␈α
the␈α
same␈α
pname␈αis␈α
already␈α
known.␈α
 Thus␈αit
␈↓ ↓H␈↓always produces an atom that is not ␈↓αeq␈↓ any already existing atom.

␈↓ ↓H␈↓        ␈↓↓gensym␈↓␈α
behaves␈α
similarly,␈α
but␈α
it␈α
takes␈αno␈α
arguments.␈α
 It␈α
determines␈α
the␈α
pname␈α
from␈αa␈α
prefix
␈↓ ↓H␈↓and␈α
a␈αcount␈α
stored␈α
in␈αsome␈α
gobal␈αlocation.␈α
 The␈α
usual␈αprefix␈α
is␈α␈↓¬G␈α
␈↓␈α
and␈αthe␈α
resulting␈α
pname␈αhas
␈↓ ↓H␈↓the␈α∂form␈α∂␈↓¬Gdddd␈α∂␈↓where␈α∂each␈α∂␈↓¬d␈α∂␈↓is␈α∂a␈α∞decimal␈α∂digit.␈α∂ Each␈α∂time␈α∂␈↓↓gensym␈↓␈α∂is␈α∂evaluated␈α∂the␈α∂count␈α∞is
␈↓ ↓H␈↓incremented␈α⊂by␈α⊂1.␈α⊂ Thus␈α⊂if␈α⊂we␈α⊂type␈α⊂␈↓¬(GENSYM)␈α⊂␈↓to␈α⊂LISP␈α⊂and␈α⊂it␈α⊂types␈α⊂back␈α⊂␈↓¬GOO69␈α⊃␈↓then␈α⊂typing
␈↓ ↓H␈↓␈↓¬(GENSYM)␈α∞␈↓again␈α
will␈α∞result␈α
in␈α∞␈↓¬G0070␈α
␈↓being␈α∞typed␈α
back.␈α∞ (No␈α
prediction␈α∞is␈α
made␈α∞as␈α
to␈α∞what␈α
will
␈↓ ↓H␈↓happen␈α⊃when␈α⊃the␈α⊃symbol␈α⊂reaches␈α⊃␈↓¬G9999␈↓).␈α⊃ (LISP␈α⊃implementations␈α⊂usually␈α⊃provide␈α⊃the␈α⊃user␈α⊂a
␈↓ ↓H␈↓mechanism␈α⊃for␈α∩resetting␈α⊃the␈α⊃␈↓¬GENSYM␈α∩␈↓count␈α⊃and␈α⊃prefix␈α∩character(s).)␈α⊃ Note␈α∩the␈α⊃non-functional
␈↓ ↓H␈↓aspect␈α∀of␈α∃␈↓↓gensym.␈↓␈α∀ Namely,␈α∃␈↓↓gensym[]≠gensym[].␈↓␈α∀ (Try␈α∀typing␈α∃␈↓¬(EQ␈α∀(GENSYM)␈α∃(GENSYM))␈↓␈α∀to
␈↓ ↓H␈↓LISP.) ␈↓↓gensym␈↓ is useful in places where you need a label, but don't care what it is called.

␈↓ ↓H␈↓        ␈↓↓implode,␈↓␈α
like␈α∞␈↓↓maknam␈↓␈α
takes␈α∞a␈α
list␈α∞of␈α
characters␈α∞and␈α
returns␈α∞an␈α
atom␈α∞whose␈α
pname␈α∞is␈α
the
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠129


␈↓ ↓H␈↓concatenation␈α
of␈α
the␈α
characters␈α
in␈α
the␈α
list.␈α
 However,␈α
unlike␈α
␈↓↓maknam␈↓␈α
it␈α
first␈α
looks␈α
to␈α
see␈α
if␈αan␈α
atom
␈↓ ↓H␈↓with␈αthat␈αpname␈αalready␈αknown␈α(in␈αthe␈αoblist)␈αand␈αif␈αso␈αreturns␈αthat␈αatom␈α(not␈αa␈αnew␈αone)␈αIf␈αnot,
␈↓ ↓H␈↓a␈αnew␈αatom␈αis␈αmade␈α
with␈αthe␈αappropriate␈αpname␈αand␈αentered␈α
in␈αthe␈αoblist.␈α Thus␈α␈↓↓implode␈↓␈α
is␈αthe
␈↓ ↓H␈↓same␈αas␈α
applying␈α␈↓↓readatom␈↓␈α
to␈αthe␈α
pname␈αcomputed␈αfrom␈α
the␈αcharacter␈α
list.␈α It␈α
behaves␈αmore␈αlike␈α
a
␈↓ ↓H␈↓function than do ␈↓↓maknam␈↓ and ␈↓↓gensym.␈↓

␈↓ ↓H␈↓        ␈↓↓explode␈↓␈αis␈αessentially␈αan␈αinverse␈αto␈α␈↓↓implode.␈↓␈α It␈αtakes␈αan␈αatom␈αand␈αreturns␈αthe␈αcharacter␈αlist
␈↓ ↓H␈↓corresponding to the pname of the atom.

␈↓ ↓H␈↓        One␈α
reason␈α
for␈α
using␈α
␈↓↓gensym␈↓␈α
and␈α
␈↓↓maknam␈↓␈α
has␈αto␈α
do␈α
with␈α
the␈α
fact␈α
that␈α
an␈α
atomic␈αsymbol
␈↓ ↓H␈↓that␈αis␈αput␈αon␈αthe␈αoblist␈αnever␈αgoes␈α
away␈α(unless␈αyou␈αexplicitly␈α␈↓↓remob␈↓␈αit),␈αwhile␈αatoms␈αgenerated␈α
by
␈↓ ↓H␈↓␈↓↓gensym␈↓␈α∞and␈α∞␈↓↓implode␈↓␈α∞can␈α∞be␈α∞garbage␈α∞collected␈α∞as␈α∞soon␈α∞as␈α∞no␈α∞one␈α∞is␈α∞pointing␈α∞to␈α∞them␈α∂any␈α∞more.
␈↓ ↓H␈↓Thus␈α
if␈α
you␈α
have␈α
a␈α
progran␈α
that␈α
is␈α
going␈αto␈α
generate␈α
a␈α
lot␈α
of␈α
temporary␈α
symbols␈α
which␈α
will␈αnot␈α
be
␈↓ ↓H␈↓wanted after some it is efficient spacewise to not put these symbols on the oblist.

␈↓ ↓H␈↓        If␈α∞you␈α
are␈α∞confused␈α
at␈α∞this␈α∞point,␈α
some␈α∞examples␈α
of␈α∞the␈α
behaviour␈α∞of␈α∞␈↓↓maknam,␈↓␈α
␈↓↓implode,␈↓
␈↓ ↓H␈↓␈↓↓gensym,␈↓␈α␈↓↓intern,␈↓␈α␈↓↓remob,␈↓␈αand␈α␈↓↓explode␈↓␈αmy␈αhelp.␈α Your␈αare␈αstrongly␈αencourage␈αto␈αgo␈αto␈αyour␈αnearest
␈↓ ↓H␈↓LISP and try some experiments of your own to be sure you understand what is happening.

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(EQ (MAKNAM '(B A M)) (MAKNAM '(B A M)))␈↓
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬NIL  ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(EQ (IMPLODE '(B A M)) (IMPLODE '(B A M)))␈↓
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬T  ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(SETQ X (MAKNAM '(F O O)))␈↓
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬FOO  ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(EXPLODE X)␈↓
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬(F O O) ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(EXPLODE 'FOO)␈↓
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬(F O O) ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(EQ X (MAKNAM '(F O O)))␈↓␈↓ λ(;␈↓↓maknam␈↓ makes a new atom
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬NIL  ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(EQ X (IMPLODE '(F O O)))␈↓␈↓ λ(;␈↓↓maknam␈↓ didn't intern ␈↓↓x␈↓
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬NIL  ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(EQ 'FOO (IMPLODE '(F O O)))␈↓␈↓ λ(; ␈↓↓readatom␈↓ interns ␈↓¬FOO ␈↓
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬T  ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(EQ X 'FOO)␈↓
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬NIL  ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(SETQ Y (IMPLODE '(F O X)))␈↓
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬FOX  ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(EQ Y (IMPLODE '(F O X)))␈↓␈↓ λ(;␈↓↓implode␈↓ interned ␈↓↓y␈↓
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬T  ␈↓
␈↓ ↓H␈↓130␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(EQ Y (INTERN Y))␈↓
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬T  ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(SETQ Y (MAKNAM '(F O X)))␈↓
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬FOX  ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(EQ Y (INTERN Y))␈↓␈↓ λ(;an atom having same pname
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬NIL ␈↓␈↓ λ(;but not ␈↓αeq␈↓ ␈↓↓y␈↓ is on the oblist

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(REMOB Y)␈↓
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬NIL ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬Y␈↓␈↓ λ(;␈↓↓y␈↓ is still a prefectly good atom
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬FOX ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(EQ Y (IMPLODE '(F O X)))␈↓␈↓ λ(; but not on the oblist any more
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬NIL ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(SETQ X (GENSYM))␈↓
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬G0123  ␈↓

␈↓ ↓H␈↓␈↓ αλuser:␈↓ β(␈↓¬(EQ X 'G0123)␈↓␈↓ λ(;␈↓↓gensym␈↓ also makes new
␈↓ ↓H␈↓␈↓ αλLISP:␈↓ β(␈↓¬NIL ␈↓␈↓ λ(;uninterned atoms


␈↓ ↓H␈↓αList structure.

␈↓ ↓H␈↓                Now␈α
that␈α
we␈α
can␈αbuild␈α
and␈α
recognize␈α
atoms,␈αthe␈α
next␈α
step␈α
is␈αto␈α
be␈α
able␈α
to␈αconstruct
␈↓ ↓H␈↓representations␈α⊗of␈α⊗arbitrary␈α⊗S-expressions.␈α⊗ Thus␈α∃we␈α⊗need␈α⊗to␈α⊗represent␈α⊗list␈α⊗structures␈α∃and
␈↓ ↓H␈↓implement␈α
the␈α
operations␈α␈↓↓car,␈↓␈α
␈↓↓cdr,␈↓␈α
and␈α␈↓↓cons.␈↓␈α
 LISP␈α
sets␈αaside␈α
a␈α
portion␈αof␈α
memory␈α
available␈αto␈α
it
␈↓ ↓H␈↓in␈αan␈αarea␈αcalled␈αlist␈αspace.␈α This␈αspace␈αcontain␈αof␈αcons-cells.␈α As␈αexplained␈αin␈αChapter␈αI,␈αa␈αcons-
␈↓ ↓H␈↓cell␈α
must␈α
be␈α
big␈α
enough␈α
to␈αhold␈α
two␈α
pointers␈α
and␈α
could␈α
be␈αa␈α
single␈α
machine␈α
word␈α
with␈α
the␈αtwo
␈↓ ↓H␈↓halves␈αbeing␈αpointers␈αto␈αthe␈αa-␈αand␈αd-␈αparts␈αor␈αif␈αthe␈αsize␈αof␈αa␈αword␈αis␈αtoo␈αsmall,␈αa␈αpair␈αof␈α
words.
␈↓ ↓H␈↓In␈α⊂either␈α⊂case␈α⊂␈↓↓car␈↓␈α⊂and␈α⊃␈↓↓cdr␈↓␈α⊂can␈α⊂be␈α⊂fairly␈α⊂simply␈α⊂operations␈α⊃(in␈α⊂the␈α⊂case␈α⊂of␈α⊂the␈α⊃usual␈α⊂PDP-10
␈↓ ↓H␈↓implementation a single machine instruction).

␈↓ ↓H␈↓        At␈αany␈α
particular␈αtime␈αsome␈α
of␈αthe␈α
list␈αspace␈αwill␈α
be␈αin␈α
use␈αand␈αsome␈α
of␈αit␈α
will␈αbe␈αfree.␈α
 The
␈↓ ↓H␈↓free␈αspace␈αis␈αlinked␈αinto␈αa␈αsingle␈αlist␈αcalled␈αthe␈α"free␈αstorage␈αlist"␈αand␈αLISP␈αkeeps␈αa␈αpointer␈αto␈αthe
␈↓ ↓H␈↓beginning␈αof␈αthat␈αlist.␈α When␈αa␈α␈↓↓cons␈↓␈αis␈αexecuted,␈αa␈αword␈αis␈αremoved␈αfrom␈αthe␈αfree␈αstorage␈αlist,␈αthe
␈↓ ↓H␈↓a-part␈α
and␈α
the␈αd-part␈α
are␈α
filled␈α
in␈αand␈α
a␈α
pointer␈α
to␈αthis␈α
word␈α
is␈α
returned.␈α When␈α
there␈α
is␈αno␈α
more
␈↓ ↓H␈↓free␈αstorage␈αleft␈α(more␈αaccurately␈αwhen␈αthe␈α
amount␈αleft␈αreaches␈αsome␈αpreset␈αminimal␈αamount)␈α
then
␈↓ ↓H␈↓LISP␈αattempts␈αto␈αobtain␈αmore␈αfree␈αspace.␈α One␈αway␈αis␈αto␈αrequest␈αit␈αfrom␈αthe␈αhost␈αsystem,␈αbut␈αthis
␈↓ ↓H␈↓is␈α
undesirable␈αin␈α
general,␈αsince␈α
a␈α
lot␈αof␈α
the␈αlist␈α
space␈αis␈α
taken␈α
up␈αby␈α
structures␈αwhich␈α
are␈αno␈α
longer
␈↓ ↓H␈↓accessible␈α
and␈α
may␈α
as␈α∞well␈α
be␈α
"recycled".␈α
 This␈α∞recycling␈α
process␈α
is␈α
done␈α∞by␈α
what␈α
is␈α
known␈α∞as␈α
a
␈↓ ↓H␈↓"garbage␈αcollector".␈α Thus␈αwhen␈α␈↓↓cons␈↓␈αdiscovers␈αthat␈αit␈αis␈αout␈αof␈αspace,␈αit␈αcalls␈αthe␈αgarbage␈αcollecter
␈↓ ↓H␈↓to␈α⊂find␈α⊂some.␈α⊂ The␈α⊂garbage␈α⊃collector␈α⊂must␈α⊂first␈α⊂find␈α⊂out␈α⊂which␈α⊃parts␈α⊂of␈α⊂list␈α⊂space␈α⊂are␈α⊃in␈α⊂use
␈↓ ↓H␈↓(accessible)␈α∂and␈α∂which␈α∂are␈α∂not.␈α∂  This␈α∂is␈α∂known␈α∂as␈α∂the␈α∂marking␈α∂phase.␈α∂ In␈α∂the␈α∂sweep␈α⊂phase␈α∂it
␈↓ ↓H␈↓collects␈αthe␈αunused␈αspace,␈αadding␈αthe␈αrecovered␈αcons-cells␈αto␈αthe␈αfree␈αstorage␈αlist.␈αIf␈αit␈α
succeeds␈αin
␈↓ ↓H␈↓finding␈αunused␈α
space,␈α␈↓↓cons␈↓␈α
can␈αproceed,␈α
if␈αnot␈α
the␈αcomputation␈α
usually␈αgrinds␈α
to␈αa␈α
halt,␈αprinting
␈↓ ↓H␈↓some suitable error message.
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠131


␈↓ ↓H␈↓        To␈αdetermine␈αwhat␈αparts␈αof␈αlist␈αstructure␈αare␈αcurrently␈αaccessible␈αthe␈αgarbage␈αcollector␈αstarts
␈↓ ↓H␈↓with␈α⊃all␈α⊂of␈α⊃the␈α⊂immediately␈α⊃accessible␈α⊂parts␈α⊃and␈α⊂follows␈α⊃all␈α⊂possible␈α⊃␈↓↓car␈↓␈α⊂and␈α⊃␈↓↓cdr␈↓␈α⊃chains␈α⊂until
␈↓ ↓H␈↓reaching␈αan␈αatom␈αor␈αsomething␈αpreviously␈α
seen␈α(marked).␈α The␈αimmediately␈αaccessible␈αparts␈αof␈α
list
␈↓ ↓H␈↓structure␈αinclude␈αthe␈αproperty␈αlists␈αof␈αall␈αatoms␈α(including␈αthe␈αcontents␈αof␈αvalue␈αcell␈αin␈αcase␈αthis␈αis
␈↓ ↓H␈↓kept␈αseparately),␈αstructures␈αon␈αwhatever␈αstacks␈αLISP␈α
is␈αusing␈αfor␈αcontrol,␈αcontents␈αof␈αany␈α
list␈αtype
␈↓ ↓H␈↓arrays,␈αplus␈αprobably␈α
a␈αfew␈αspecial␈αlocations␈α
or␈αregisters␈αthat␈αLISP␈α
may␈αuse␈αfor␈α
special␈αpurposes.
␈↓ ↓H␈↓There␈α
are␈αa␈α
number␈αof␈α
algorithms␈αof␈α
doing␈αthe␈α
marking␈αand␈α
collection␈αof␈α
unused␈αspace.␈α
 It␈α
is␈αa
␈↓ ↓H␈↓technique␈α⊂generally␈α⊂useful␈α⊂in␈α⊂situations␈α⊂where␈α⊂storage␈α⊂gets␈α⊂allocated␈α⊂and␈α⊂reclaimed.␈α⊂ For␈α∂more
␈↓ ↓H␈↓details the reader is referred to Knuth[1968a] or Baker[1978] and references therein.


␈↓ ↓H␈↓αOther LISP objects.

␈↓ ↓H␈↓        The␈α∞structures␈α∂and␈α∞operations␈α∞we␈α∂have␈α∞discussed␈α∂above␈α∞are␈α∞sufficient␈α∂for␈α∞a␈α∂LISP␈α∞system
␈↓ ↓H␈↓that␈α∂is␈α∂only␈α∂going␈α∂to␈α∂deal␈α∂with␈α∂interpreted␈α∂code␈α∂and␈α∂will␈α∂only␈α∂allow␈α∂S-expressions␈α∂as␈α∂data␈α∞for
␈↓ ↓H␈↓programs.␈α⊃ Most␈α⊃systems␈α⊃provide␈α⊃at␈α⊃least␈α⊃two␈α⊂additional␈α⊃features.␈α⊃  One␈α⊃is␈α⊃the␈α⊃ability␈α⊃to␈α⊂run
␈↓ ↓H␈↓compiled␈α
code,␈αand␈α
the␈αother␈α
is␈αthe␈α
addition␈αof␈α
arrays␈α
to␈αthe␈α
allowed␈αtypes␈α
of␈αdata.␈α
  Thus␈αa␈α
space
␈↓ ↓H␈↓known␈αas␈αbinary␈αprogram␈αspace␈αis␈αusually␈αset␈αaside␈αfor␈αstoring␈αcompiled␈αcode.␈α  Sometimes␈αarrays
␈↓ ↓H␈↓are␈αkept␈αin␈αthe␈αsame␈αspace,␈αand␈αsometimes␈αthey␈αare␈αkept␈αin␈αan␈αadditional␈αseparate␈αspace.␈α In␈αany
␈↓ ↓H␈↓event,␈αthis␈αmeans␈αthat␈αthere␈αare␈αtwo␈αadditional␈αobjects␈αthat␈αsome␈αof␈αthe␈αbasic␈αLISP␈αroutines␈αmust
␈↓ ↓H␈↓recognize␈α
and␈α∞deal␈α
with.␈α
 One␈α∞is␈α
the␈α∞"subr"␈α
object␈α
which␈α∞is␈α
the␈α
address␈α∞of␈α
a␈α∞compiled␈α
program
␈↓ ↓H␈↓and␈α∞is␈α∞associated␈α∞with␈α∞the␈α∞atom␈α∞naming␈α∞the␈α∞program␈α∞ via␈α∞the␈α∞␈↓¬SUBR␈α∞␈↓property.␈α∞ The␈α∞other␈α∞is␈α∞an
␈↓ ↓H␈↓array␈α⊂descriptor␈α⊂which␈α⊂will␈α⊂be␈α⊂associated␈α⊃via␈α⊂the␈α⊂␈↓¬ARRAY␈α⊂␈↓property␈α⊂with␈α⊂the␈α⊂atom␈α⊃naming␈α⊂that
␈↓ ↓H␈↓array.

␈↓ ↓H␈↓        In␈α
addition␈α
to␈α
the␈α
structures␈α
that␈α
make␈α∞up␈α
the␈α
data␈α
manipulated␈α
by␈α
a␈α
LISP␈α∞program,␈α
the
␈↓ ↓H␈↓LISP␈α
system␈αmust␈α
maintain␈αstructures␈α
describing␈αthe␈α
current␈αstate␈α
of␈αa␈α
computation.␈α
  In␈αgeneral
␈↓ ↓H␈↓there␈αwill␈α
be␈αone␈α
or␈αmore␈α
stacks␈α(often␈α
called␈αPush-Down-Lists␈α
or␈αpdls)␈α
where␈αinformation␈αis␈α
kept
␈↓ ↓H␈↓as␈α∞to␈α∞current␈α
and/or␈α∞old␈α∞variable␈α
bindings␈α∞(binding␈α∞contexts␈α
or␈α∞ environments),␈α∞knowlege␈α
about
␈↓ ↓H␈↓the␈α∂expression␈α∂currently␈α∂being␈α∂evaluated,␈α∂and␈α∂what␈α∂to␈α∂do␈α∂next␈α∂(control␈α∂stack).␈α∂ Information␈α∞on
␈↓ ↓H␈↓these␈αstacks␈αis␈αaccessed␈αvia␈αpdl-pointers,␈αcontext␈αpointers␈αetc..␈α The␈αLISP␈αsystem␈αusually␈αprovides
␈↓ ↓H␈↓programs␈α
to␈α
allow␈α
the␈α
user␈α
to␈α
examine␈α
control␈α
stacks␈α
and␈α
environments.␈α
 We␈α
will␈α
say␈αsomething
␈↓ ↓H␈↓about this later.

␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓1.␈α Write␈αa␈αprogram␈α␈↓↓saveup␈↓␈αthat␈αtakes␈αa␈αlist␈α
of␈αatoms␈αand␈αa␈αlist␈αof␈αproperties␈αand␈αcollects␈αfor␈α
each
␈↓ ↓H␈↓atom and association list of property/value pairs.  ␈↓↓saveup␈↓ returns a list of atom/alist pairs.

␈↓ ↓H␈↓2.␈α⊃ Write␈α⊃a␈α⊃program␈α⊃␈↓↓removem␈↓␈α⊃that␈α⊃takes␈α⊃the␈α⊃same␈α⊃arguments␈α⊃as␈α⊃␈↓↓saveup␈↓␈α⊃and␈α⊃removes␈α⊃all␈α⊂the
␈↓ ↓H␈↓properties from the property lists of all the atoms.

␈↓ ↓H␈↓3.␈α Write␈αa␈αprogram␈α␈↓↓restorem␈↓␈αthat␈αtakes␈αa␈αlist␈αof␈αthe␈αform␈αoutput␈αby␈α␈↓↓saveup␈↓␈αand␈αputs␈αback␈αall␈αof
␈↓ ↓H␈↓the properties on the property lists of all the atoms.

␈↓ ↓H␈↓The␈α∞above␈α∞collection␈α∞of␈α∞functions␈α∞are␈α∂useful␈α∞for␈α∞switching␈α∞environments␈α∞in␈α∞a␈α∞system␈α∂written␈α∞in
␈↓ ↓H␈↓LISP.
␈↓ ↓H␈↓132␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓2.  ␈↓αThe Top level of LISP.␈↓


␈↓ ↓H␈↓        The␈αtop␈αlevel␈αof␈αLISP␈αcan␈αbe␈αdescribed␈αin␈αterms␈αof␈αthe␈αprograms␈α␈↓↓read,␈↓␈α␈↓↓eval␈↓␈αand␈α␈↓↓print␈↓␈αfor
␈↓ ↓H␈↓reading, evaluating and printing S-expressions.  Namely,

␈↓ ↓H␈↓␈↓ β\␈↓↓toplevel[] ← ␈↓αprogram␈↓↓[[] top: print eval read[]; ␈↓αgo to␈↓↓ top]␈↓. 

␈↓ ↓H␈↓The␈α⊂␈↓↓read␈↓␈α⊂program␈α∂gets␈α⊂from␈α⊂the␈α⊂input␈α∂stream␈α⊂the␈α⊂next␈α⊂string␈α∂of␈α⊂tokens␈α⊂corresponding␈α⊂to␈α∂the
␈↓ ↓H␈↓external␈α↔representation␈α⊗of␈α↔an␈α⊗S-expression␈α↔and␈α⊗constructs␈α↔the␈α⊗corresponding␈α↔list␈α⊗structure
␈↓ ↓H␈↓representation.␈α∞ ␈↓↓eval␈↓␈α∞computes␈α∞the␈α∞value␈α∂denoted␈α∞by␈α∞the␈α∞expression␈α∞in␈α∞the␈α∂current␈α∞environment,
␈↓ ↓H␈↓and␈α∩ ␈↓↓print␈↓␈α∩converts␈α⊃a␈α∩list␈α∩structure␈α⊃into␈α∩the␈α∩external␈α⊃representation␈α∩of␈α∩the␈α∩corresponding␈α⊃S-
␈↓ ↓H␈↓expression and sends it to the output stream.

␈↓ ↓H␈↓        In␈αthis␈αChapter␈α
we␈αwill␈αpresent␈αprograms␈α
that␈αembody␈αthe␈α
recursive␈αstructure␈αof␈αthe␈α
process
␈↓ ↓H␈↓of␈αreading␈αand␈αprinting␈αS-expressions␈α
in␈αboth␈αof␈αthe␈αnotations␈α
(dot␈αand␈αlist)␈αdiscussed␈αin␈α
Chapter
␈↓ ↓H␈↓I.␈α∂ The␈α∂"slashifying"␈α∂of␈α∂special␈α∂characters␈α∂is␈α∞also␈α∂discussed.␈α∂ We␈α∂have␈α∂already␈α∂discussed␈α∂how␈α∞a
␈↓ ↓H␈↓simplified␈α
version␈α
of␈α␈↓↓eval␈↓␈α
works␈α
(Chapter␈αI␈α
␈↓π∞␈↓13).␈α
  Here␈αwe␈α
discuss␈α
some␈αof␈α
the␈α
decisions␈αthat␈α
must
␈↓ ↓H␈↓be␈αmade␈αin␈α
desigining␈αa␈αLISP␈α
interpreter,␈αhow␈αthey␈α
effect␈αthe␈αresults␈α
computed␈αby␈αyour␈α
programs,
␈↓ ↓H␈↓and how LISP uses the "system properties" stored on the property lists of atoms.


␈↓ ↓H␈↓αReading and Printing: "dot" notation

␈↓ ↓H␈↓        Consider␈αfirst␈αreading␈αand␈αprinting␈αS-expressions␈αin␈α"dot"␈αnotation.␈α Rather␈αthan␈αdeal␈αwith
␈↓ ↓H␈↓the␈α
issue␈αof␈α
representing␈α
character␈αstrings,␈α
we␈αread␈α
an␈α
print␈αa␈α
listified␈α
external␈αnotation␈α
which␈αis␈α
a
␈↓ ↓H␈↓list␈α∞of␈α∞atoms.␈α∞ The␈α∞special␈α∞atoms␈α∞␈↓¬LP,␈α∞␈↓␈↓¬RP␈α∂␈↓and␈α∞␈↓¬DOT␈α∞␈↓represent␈α∞the␈α∞delimiter␈α∞tokens␈α∞"(",␈α∞")"␈α∂and␈α∞"."
␈↓ ↓H␈↓respectively and all other atoms represent themselves.

␈↓ ↓H␈↓        Let␈α∞␈↓↓DOTLIST␈↓␈α∞be␈α∞the␈α∞sort␈α∞of␈α∞lists␈α∞that␈α∞correspond␈α∞to␈α∞"dot"␈α∞notation␈α∞representations␈α∂of␈α∞S-
␈↓ ↓H␈↓expressions.   They can be characterized as follows:

␈↓ ↓H␈↓(i)␈α␈↓↓<␈α␈↓¬<other-atom>␈↓↓␈α>␈↓␈αis␈αa␈α␈↓↓DOTLIST␈↓␈αrepresenting␈αthe␈αatom␈α␈↓¬<other-atom>␈↓,␈αwhere␈α
␈↓¬<other-atom>␈↓
␈↓ ↓H␈↓is any atom not in the list of special atoms ␈↓¬LP, ␈↓␈↓¬DOT, ␈↓␈↓¬RP. ␈↓

␈↓ ↓H␈↓(ii)␈α
if␈α
␈↓↓da␈↓␈α
and␈α
␈↓↓dd␈↓␈α
are␈α
␈↓↓DOTLIST␈↓s␈α
then␈α
␈↓↓<␈↓¬LP␈↓↓>␈α*␈α
da␈α
*␈α
<␈↓¬DOT␈↓↓>␈α
*␈α
dd␈α
*␈α
<␈↓¬RP␈↓↓>␈↓␈α
is␈α
a␈α␈↓↓DOTLIST␈↓␈α
representing
␈↓ ↓H␈↓␈↓↓[x . y]␈↓  where ␈↓↓da␈↓ represents ␈↓↓x␈↓ and ␈↓↓dd␈↓ represents ␈↓↓y.␈↓

␈↓ ↓H␈↓(iii) Those are all of the lists of sort ␈↓↓DOTLIST.␈↓

␈↓ ↓H␈↓The␈αprogram␈α␈↓↓prindot␈↓␈α
"print"s␈αa␈αlist␈α
of␈αsort␈α␈↓↓DOTLIST␈↓␈αrepresenting␈α
the␈αS-expression␈αgiven␈α
as␈αan
␈↓ ↓H␈↓argument.

␈↓ ↓H␈↓The definition of ␈↓↓prindot␈↓ is:

␈↓ ↓H␈↓↓2.1)␈↓ β8prindot[e] ← prina[e,␈↓¬NIL␈↓↓]

␈↓ ↓H␈↓↓␈↓ β8prina[e, l] ←
␈↓ ↓H␈↓↓2.2)␈↓ βx␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ e . l
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ ␈↓¬LP␈↓↓ . prina[␈↓αa|␈↓↓e, ␈↓¬DOT␈↓↓ . prina[␈↓αd|␈↓↓e, ␈↓¬RP␈↓↓ . l]]
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠133


␈↓ ↓H␈↓thus

␈↓ ↓H␈↓␈↓ β8␈↓↓prindot[␈↓¬(PLUS (TIMES A B) C)␈↓↓] = ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓¬(LP PLUS DOT LP LP TIMES DOT LP A DOT LP B ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓¬ DOT NIL RP RP RP DOT LP C DOT NIL RP RP RP),␈↓

␈↓ ↓H␈↓        Notice␈α∞that␈α∂the␈α∞recursive␈α∂structure␈α∞of␈α∂␈↓↓prindot␈↓␈α∞is␈α∂very␈α∞much␈α∂like␈α∞that␈α∂of␈α∞␈↓↓flatten␈↓␈α∂(I.8.8).␈α∞ It
␈↓ ↓H␈↓simply inserts the delimiter characters in the appropriate places.

␈↓ ↓H␈↓        The␈α∂program␈α∞␈↓↓readdot␈↓␈α∂"read"s␈α∂a␈α∞list␈α∂of␈α∂atoms.␈α∞ If␈α∂the␈α∞list␈α∂is␈α∂of␈α∞the␈α∂form␈α∂␈↓↓dl␈α∞*␈α∂u␈↓␈α∂for␈α∞some
␈↓ ↓H␈↓␈↓↓DOTLIST,␈↓␈α
␈↓↓dl␈↓␈α
and␈α
list␈α
␈↓↓u␈↓␈α
then␈α
the␈α
result␈α
is␈α
␈↓↓e . u␈↓␈α
where␈α
␈↓↓e␈↓␈α
is␈α
the␈α
S-expression␈α
represented␈α
by␈α␈↓↓dl.␈↓
␈↓ ↓H␈↓Otherwise the atom ␈↓¬READ-ERROR ␈↓is returned.

␈↓ ↓H␈↓The definition of ␈↓↓readdot␈↓ is:

␈↓ ↓H␈↓↓␈↓ αλreaddot u ←
␈↓ ↓H␈↓↓␈↓ βλ␈↓αif␈↓↓ ␈↓αat|␈↓↓u ␈↓αthen␈↓↓ ␈↓¬READ-ERROR␈↓↓
␈↓ ↓H␈↓↓␈↓ βλ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u ␈↓αequal␈↓↓ ␈↓¬LP␈↓↓ ␈↓αthen␈↓↓
␈↓ ↓H␈↓↓2.3)␈↓ βH{readdot ␈↓αd|␈↓↓u}[λw:␈↓ λ(␈↓;read a-part␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧λ␈↓αif␈↓↓ ␈↓αat|␈↓↓w ∨ ¬[␈↓αad|␈↓↓w ␈↓αequal␈↓↓ ␈↓¬DOT␈↓↓] ␈↓αthen␈↓↓ ␈↓¬READ-ERROR␈↓↓␈↓ λ(␈↓;check for error␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧λ␈↓αelse␈↓↓ {readdot ␈↓αdd|␈↓↓w}[λv:␈↓ λ(␈↓;read d part␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧H␈↓αif␈↓↓ ␈↓αat|␈↓↓v ∨ ¬[␈↓αad|␈↓↓v ␈↓αequal␈↓↓ ␈↓¬RP␈↓↓] ␈↓αthen␈↓↓ ␈↓¬READ-ERROR␈↓↓␈↓ λ(␈↓;check for error␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧H␈↓αelse␈↓↓ [␈↓αa|␈↓↓w . ␈↓αa|␈↓↓v] . ␈↓αdd|␈↓↓v]]␈↓ λ(␈↓;␈↓↓cons␈↓ up the result␈↓↓
␈↓ ↓H␈↓↓␈↓ βλ␈↓αelse␈↓↓ u


␈↓ ↓H␈↓thus

␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αa|␈↓↓readdot[␈↓¬(LP PLUS DOT LP LP TIMES DOT LP A DOT LP B ␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧X␈↓↓␈↓¬ DOT NIL RP RP RP DOT LP C DOT NIL RP RP RP)␈↓↓] = ␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓¬(PLUS (TIMES A B) C)␈↓.


␈↓ ↓H␈↓αReading and Printing: mixed "list-dot" notation

␈↓ ↓H␈↓        Now␈α∞we␈α∞treat␈α∞the␈α∞more␈α∞general␈α∞mixed␈α∞"list-dot"␈α∞notation.␈α∞ Let␈α∞␈↓↓LDOTLIST␈↓␈α∞be␈α∞the␈α∞sort␈α
of
␈↓ ↓H␈↓lists␈α∃that␈α∃correspond␈α∃to␈α∃"list-dot"␈α∃notation␈α∃representations␈α∃of␈α∃S-expressions.␈α∃  They␈α⊗can␈α∃be
␈↓ ↓H␈↓characterized as follows:

␈↓ ↓H␈↓(i)␈α⊂␈↓↓<␈α⊂␈↓¬<other-atom>␈↓↓␈α⊂>␈↓␈α⊂is␈α⊂a␈α⊂␈↓↓LDOTLIST␈↓␈α⊂representing␈α⊂the␈α⊂atom␈α⊂␈↓¬<other-atom>␈↓,␈α⊂where␈α∂␈↓¬<other-
␈↓ ↓H␈↓¬atom>␈↓ is any atom not in the list of special atoms ␈↓¬LP, ␈↓␈↓¬DOT, ␈↓␈↓¬RP. ␈↓

␈↓ ↓H␈↓if ␈↓↓di␈↓ ␈↓↓1≤i≤n␈↓ and ␈↓↓dd␈↓ are ␈↓↓LDOTLIST␈↓s representing the S-expressions ␈↓↓xi␈↓ and ␈↓↓xd␈↓  then

␈↓ ↓H␈↓(iia)  ␈↓↓<␈↓¬LP␈↓↓ ␈↓¬RP␈↓↓>␈↓ is an ␈↓↓LDOTLIST␈↓ representing ␈↓¬()␈↓ or ␈↓¬NIL␈↓.

␈↓ ↓H␈↓(iib) ␈↓↓<␈↓¬LP␈↓↓> * d1 * ... * dn * <␈↓¬RP␈↓↓>␈↓ is an ␈↓↓LDOTLIST␈↓ representing ␈↓↓<x1 ... xn>␈↓

␈↓ ↓H␈↓(iic)␈α!␈↓↓<␈↓¬LP␈↓↓>␈α!*␈α!d1␈α!...␈α!dn␈α!*␈α <␈↓¬DOT␈↓↓>␈α!*␈α!dd␈α!*␈α!<␈↓¬RP␈↓↓>␈↓␈α!is␈α!a␈α!␈↓↓DOTLIST␈↓␈α representing
␈↓ ↓H␈↓␈↓↓[x1 . [ ... [xn . xd] ... ]]␈↓
␈↓ ↓H␈↓134␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓(iii) Those are all of the lists of sort ␈↓↓DOTLIST.␈↓

␈↓ ↓H␈↓        The␈α
program␈α␈↓↓prinlis␈↓␈α
"print"s␈α
an␈αS-expression␈α
in␈α"list"␈α
notation.␈α
 This␈αis␈α
the␈α
extreme␈αform
␈↓ ↓H␈↓of␈αlist-dot␈αnotation␈αwhere␈α␈↓↓dd␈↓␈α
following␈α␈↓¬DOT␈αin␈α␈↓clause␈α(iic)␈α
must␈αbe␈αan␈αatom.␈α To␈αprint␈α
non-atomic
␈↓ ↓H␈↓S-expression␈α∞in␈α
this␈α∞style,␈α
start␈α∞by␈α
printing␈α∞␈↓¬LP,␈α
␈↓then␈α∞␈↓↓cdr␈↓␈α
through␈α∞the␈α
S-expression␈α∞printing␈α
the
␈↓ ↓H␈↓␈↓↓car␈↓␈α
until␈α
an␈α
atom␈α
is␈α
reached.␈α If␈α
this␈α
atom␈α
is␈α
␈↓¬NIL␈↓␈α
the␈αS-expression␈α
is␈α
indeed␈α
a␈α
list␈α
and␈αthe␈α
printing
␈↓ ↓H␈↓is␈α⊃terminated␈α⊃with␈α⊃␈↓¬RP.␈α⊂␈↓Otherwise␈α⊃the␈α⊃printing␈α⊃is␈α⊃terminated␈α⊂with␈α⊃␈↓¬DOT␈α⊃␈↓followed␈α⊃by␈α⊃the␈α⊂atom
␈↓ ↓H␈↓followed by ␈↓¬RP. ␈↓ ␈↓↓prinlis␈↓ is defined by:

␈↓ ↓H␈↓↓2.4)␈↓ β8prinlis[e] ← prinb[e,␈↓¬NIL␈↓↓]

␈↓ ↓H␈↓↓␈↓ β8prinb[e, l] ←
␈↓ ↓H␈↓↓␈↓ βx␈↓αif␈↓↓ ␈↓αat|␈↓↓e ␈↓αthen␈↓↓ e . l
␈↓ ↓H␈↓↓2.5)␈↓ βx␈↓αelse␈↓↓ ␈↓¬LP␈↓↓ . [␈↓αif␈↓↓ ␈↓αn|␈↓↓␈↓αd|␈↓↓e ␈↓αthen␈↓↓ prinb[␈↓αa|␈↓↓e, ␈↓¬RP␈↓↓ . l]
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓␈↓αd|␈↓↓e ␈↓αthen␈↓↓ prinb[␈↓αa|␈↓↓e, ␈↓¬DOT␈↓↓ . [␈↓αd|␈↓↓e . [␈↓¬RP␈↓↓ . l]]]
␈↓ ↓H␈↓↓␈↓ ∧x␈↓αelse␈↓↓ prinb[␈↓αa|␈↓↓e, ␈↓αd|␈↓↓prinb[␈↓αd|␈↓↓e, l]]]


␈↓ ↓H␈↓thus

␈↓ ↓H␈↓␈↓ αi␈↓↓prinlis[␈↓¬(PLUS (TIMES A B) C)␈↓↓] = ␈↓¬(LP PLUS LP TIMES A B RP C RP).␈↓↓␈↓
␈↓ ↓H␈↓and
␈↓ ↓H␈↓␈↓ β(␈↓↓     prinlis[␈↓¬(A . ( B . (C . D)))␈↓↓] = ␈↓¬(LP A B C DOT D RP).␈↓↓␈↓


␈↓ ↓H␈↓␈↓↓prinlis␈↓␈αalso␈α
has␈αthe␈α
same␈αbasic␈α
structure␈αas␈α
␈↓↓prindot,␈↓␈αbut␈α
makes␈αsome␈α
intermediate␈αtests␈α
in␈αorder␈α
to
␈↓ ↓H␈↓omit all but the essential delimiter characters.

␈↓ ↓H␈↓        The␈αprogram␈α␈↓↓read␈↓␈αgobbles␈αup␈α
an␈αinitial␈αsegment␈αof␈αa␈α
list␈α␈↓↓dl * u␈↓␈αand␈αreturns␈α␈↓↓[e . u]␈↓␈αwhere␈α
␈↓↓e␈↓
␈↓ ↓H␈↓is␈αthe␈αS-expression␈αrepresented␈αby␈α␈↓↓dl␈↓␈αin␈αlist-dot␈αnotation.␈α The␈αauxiliary␈αprogram␈α␈↓↓reada␈↓␈αdoes␈αthe
␈↓ ↓H␈↓work.␈α It␈αthe␈αuses␈αthe␈αauxiliary␈αvariable␈α␈↓↓l␈↓␈αto␈αstack␈αpartial␈αresults.␈α The␈αbasic␈αidea␈αis␈αthe␈αfollowing:
␈↓ ↓H␈↓if␈α⊂the␈α∂next␈α⊂atom␈α∂is␈α⊂␈↓¬LP␈α⊂␈↓then␈α∂and␈α⊂gobble␈α∂up␈α⊂0␈α⊂or␈α∂more␈α⊂S-expressions␈α∂from␈α⊂the␈α⊂remaining␈α∂list,
␈↓ ↓H␈↓stacking␈α∂them␈α∂on␈α∂␈↓↓l,␈↓␈α∂until␈α∂a␈α∂delimiter␈α∂(␈↓¬DOT␈α∂␈↓or␈α∞␈↓¬RP)␈α∂␈↓is␈α∂reached.␈α∂ If␈α∂the␈α∂delimeter␈α∂is␈α∂␈↓¬RP␈α∂␈↓then␈α∞the
␈↓ ↓H␈↓result␈α∂is␈α∂the␈α∂␈↓↓reverse␈↓␈α∂of␈α⊂␈↓↓l.␈↓␈α∂ If␈α∂the␈α∂delimeter␈α∂is␈α∂␈↓¬DOT␈α⊂␈↓then␈α∂gobble␈α∂up␈α∂one␈α∂more␈α⊂S-expression␈α∂and
␈↓ ↓H␈↓return ␈↓↓l␈↓  reversed onto that.  The definitions of ␈↓↓read␈↓ and ␈↓↓reada␈↓ are as follows:

␈↓ ↓H␈↓↓2.6)␈↓ β8read u ← ␈↓αif␈↓↓ ␈↓αa|␈↓↓u ␈↓αequal␈↓↓ ␈↓¬LP␈↓↓ ␈↓αthen␈↓↓ reada[␈↓αd|␈↓↓u, ␈↓¬NIL␈↓↓] ␈↓αelse␈↓↓  u


␈↓ ↓H␈↓↓␈↓ β8reada[u, l] ←
␈↓ ↓H␈↓↓␈↓ βx␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ rev1[l, ␈↓¬ERROR␈↓↓] . ␈↓¬NIL␈↓↓
␈↓ ↓H␈↓↓2.7)␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u ␈↓αequal␈↓↓ ␈↓¬RP␈↓↓ ␈↓αthen␈↓↓ reverse l . ␈↓αd|␈↓↓u
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u ␈↓αequal␈↓↓ ␈↓¬LP␈↓↓ ␈↓αthen␈↓↓ {reada[␈↓αd|␈↓↓u, ␈↓¬NIL␈↓↓]}[λw: reada[␈↓αd|␈↓↓w, ␈↓αa|␈↓↓w . l]]
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αa|␈↓↓u ␈↓αequal␈↓↓ ␈↓¬DOT␈↓↓ ␈↓αthen␈↓↓ {reada[␈↓αd|␈↓↓u, ␈↓¬NIL␈↓↓]}[λw: rev1[l, ␈↓αaa|␈↓↓w] . ␈↓αd|␈↓↓w]
␈↓ ↓H␈↓↓␈↓ βx␈↓αelse␈↓↓ reada[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u . l]

␈↓ ↓H␈↓↓2.8)␈↓ β8rev1[u, v] ← ␈↓αif␈↓↓ ␈↓αn|␈↓↓u ␈↓αthen␈↓↓ v ␈↓αelse␈↓↓ rev1[␈↓αd|␈↓↓u, ␈↓αa|␈↓↓u . v]


␈↓ ↓H␈↓thus
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠135



␈↓ ↓H␈↓␈↓ β8␈↓↓␈↓αa|␈↓↓read[␈↓¬(LP PLUS LP TIMES A B RP C RP)␈↓↓=␈↓¬(PLUS (TIMES A B) C)␈↓↓ ␈↓

␈↓ ↓H␈↓We␈αnote␈αthat␈α␈↓↓read␈↓␈αand␈α␈↓↓reada␈↓␈αdon't␈αcheck␈αthe␈αsyntax␈αcompletely,␈αso␈αsome␈αnon-wellformed␈αlists␈αwill
␈↓ ↓H␈↓be "read" without complaint, although the result may be strange.

␈↓ ↓H␈↓        We␈α⊂end␈α⊂the␈α∂discussion␈α⊂of␈α⊂reading␈α⊂and␈α∂printing␈α⊂with␈α⊂some␈α∂remarks␈α⊂on␈α⊂the␈α⊂treatment␈α∂of
␈↓ ↓H␈↓special␈α
characters␈α
in␈α
LISP.␈α
 Implicit␈α
in␈α
the␈α
functioning␈α
of␈α
the␈α
LISP␈α
scanner␈α
in␈α
the␈α
fact␈αthat␈α
certain
␈↓ ↓H␈↓characters␈α∂such␈α∂as␈α∂the␈α∂delimiters␈α∂"(",␈α∂")",␈α⊂"."␈α∂and␈α∂"␈α∂"␈α∂(<space>)␈α∂are␈α∂treated␈α∂specially␈α⊂and␈α∂cannot
␈↓ ↓H␈↓directly␈α∂be␈α∂included␈α∞in␈α∂the␈α∂names␈α∂of␈α∞atoms.␈α∂ LISP␈α∂systems␈α∂usually␈α∞provide␈α∂a␈α∂way␈α∂of␈α∞including
␈↓ ↓H␈↓these␈α
special␈α
characters␈α
in␈α
an␈α
atom␈α
name␈αby␈α
designating␈α
an␈α
additional␈α
special␈α
character␈α
to␈αserve␈α
as
␈↓ ↓H␈↓an␈α∂escape␈α∂character.␈α∂  Thus␈α∂any␈α∂character␈α⊂following␈α∂the␈α∂escape␈α∂character␈α∂in␈α∂an␈α∂input␈α⊂string␈α∂is
␈↓ ↓H␈↓treated␈αas␈αan␈αordinary␈αletter.␈α This␈αis␈αofter␈αcalled␈α"slashifying"␈αas␈αthe␈αdesignated␈αescape␈αcharacter
␈↓ ↓H␈↓is␈αoften␈α
the␈α"/"␈α
character.␈α Thus␈α
␈↓¬(AB/.D)␈α␈↓is␈α
a␈αlist␈α
of␈αone␈α
element,␈αthe␈α
atom␈αwhose␈α
pname␈αis␈α
"␈↓¬AB.D␈↓"
␈↓ ↓H␈↓whereas␈α∂␈↓¬(AB.D)␈α⊂␈↓is␈α∂a␈α∂pair,␈α⊂the␈α∂a-part␈α⊂being␈α∂␈↓¬AB␈α∂␈↓and␈α⊂the␈α∂d-part␈α⊂being␈α∂␈↓¬D.␈α∂␈↓␈α⊂ One␈α∂result␈α⊂of␈α∂this
␈↓ ↓H␈↓"slashifying"␈α∂convention␈α∂is␈α∂that␈α∂many␈α∂of␈α∂the␈α∂pname␈α∂manipulating␈α∂and␈α∂printing␈α∂functions␈α∞have
␈↓ ↓H␈↓several␈αflavors␈αaccording␈αto␈αwhether␈αthe␈αname␈αor␈αthe␈αslashified␈αversion␈αis␈αbeing␈αdealt␈αwith.␈α  We
␈↓ ↓H␈↓mention␈αthe␈α
fact␈αonly␈α
to␈αmake␈αyou␈α
aware␈αof␈α
it.␈α You␈αshould␈α
consult␈αyour␈α
LISP␈αmanual␈αfor␈α
further
␈↓ ↓H␈↓details.␈α
  You␈α
should␈α
also␈α
be␈α
aware␈α
of␈α
the␈α
fact␈α
that␈α
"/"␈α
itself␈α
(or␈α
whatever␈α
the␈α∞designated␈α
escape
␈↓ ↓H␈↓character␈αis)␈αbeing␈αa␈αspecial␈αcharacter␈αis␈αgobbled␈αup␈αby␈αthe␈αLISP␈αscanner,␈αthus␈αif␈αyou␈αreally␈αwant
␈↓ ↓H␈↓the symbol "/" you must type "//".


␈↓ ↓H␈↓αEvaluation

␈↓ ↓H␈↓        In␈αLISP␈αsystems␈αthe␈αevaluator␈αdoesn't␈αrequire␈αan␈αa-list␈α(environment)␈αargument.␈α Instead␈αit
␈↓ ↓H␈↓uses␈α
the␈α
property␈α∞lists␈α
and␈α
environment␈α∞(binding␈α
context)␈α
stacks␈α∞or␈α
some␈α
equivalent␈α∞structure␈α
to
␈↓ ↓H␈↓store␈αthe␈αcorresponding␈αinformation.␈α The␈αsimple␈α␈↓↓eval␈↓␈αwas␈αgiven␈αas␈αa␈αrecursive␈αfunction␈αand␈αthus
␈↓ ↓H␈↓much␈α⊂of␈α⊃the␈α⊂control␈α⊃structure␈α⊂is␈α⊃automatic␈α⊂in␈α⊃the␈α⊂recursion␈α⊃structure.␈α⊂ Actual␈α⊃interpreters␈α⊂are
␈↓ ↓H␈↓implemented␈αiteratively,␈αand␈αuse␈αcontrol␈α
stacks␈αto␈αkeep␈αtrack␈αof␈α
what␈αthey␈αare␈αdoing␈αand␈α
what␈αis
␈↓ ↓H␈↓left␈αto␈αbe␈αdone.␈α There␈αare␈αseveral␈αimportant␈αdecisions␈αto␈αbe␈αmade␈αin␈αthe␈αdesign␈αof␈αan␈αinterpreter.
␈↓ ↓H␈↓These␈α
include␈α
the␈α
mechanism␈α
for␈α
making,␈α
saving␈α
and␈α
restoring␈α
variable␈α
bindings,␈α
how␈αto␈α
evaluate
␈↓ ↓H␈↓or␈αinterpret␈αthe␈αexpression␈αoccuring␈αin␈αfunctional␈αposition␈αof␈αan␈αapplication␈αterm,␈αwhen␈αand␈αhow
␈↓ ↓H␈↓to␈α∪bind␈α∀the␈α∪free␈α∪variables␈α∀in␈α∪function␈α∀expressions.␈α∪ (This␈α∪relates␈α∀to␈α∪the␈α∀infamous␈α∪"funarg"
␈↓ ↓H␈↓problem.)␈αIf␈αyou␈αonly␈αwrite␈αsimple␈αclean␈αpure␈αLISP␈αprograms␈αin␈αwhich␈αno␈αfree␈αvariables␈αoccur␈αin
␈↓ ↓H␈↓function␈α∂expressions␈α∂then␈α∂these␈α∂decisions␈α∂probably␈α∂won't␈α∂effect␈α∂the␈α∂meaning␈α∂of␈α∂your␈α∞programs.
␈↓ ↓H␈↓However,␈α∞most␈α
programs␈α∞are␈α
like␈α∞that.␈α
 In␈α∞the␈α
following␈α∞we␈α
discuss␈α∞some␈α
possible␈α∞decisions␈α
and
␈↓ ↓H␈↓explain␈α
the␈αuse␈α
of␈αsystem␈α
properties.␈α  You␈α
should␈αconsult␈α
the␈αmanual␈α
for␈αLISP␈α
you␈αplan␈α
to␈αuse␈α
to
␈↓ ↓H␈↓find␈α⊂out␈α⊂what␈α⊂decisions␈α⊂have␈α⊂been␈α⊂made␈α⊂in␈α⊂designing␈α⊂the␈α⊂interpreter.␈α⊂ A␈α⊂detailed␈α⊂analysis␈α⊂of
␈↓ ↓H␈↓binding␈α∂and␈α∂control␈α⊂mechanisms␈α∂in␈α∂LISP␈α⊂can␈α∂be␈α∂found␈α∂in␈α⊂Allen[1979].␈α∂ A␈α∂very␈α⊂nice␈α∂iterative
␈↓ ↓H␈↓description␈α∀of␈α∀a␈α∀LISP␈α∀like␈α∀interpreter␈α∀is␈α∀the␈α∀SCHEME␈α∀interpreter␈α∀given␈α∀in␈α∃Sussman␈α∀and
␈↓ ↓H␈↓Steele[1978].␈α It␈αalso␈αshows␈αhow␈αcontrol␈αand␈αenvironment␈αstacks␈αare␈αhandled␈αin␈αa␈αparticularly␈α
nice
␈↓ ↓H␈↓manner.

␈↓ ↓H␈↓        When␈α
LISP␈α
begins␈α
to␈α∞evaluate␈α
the␈α
application␈α
of␈α
a␈α∞λ-expression␈α
to␈α
a␈α
list␈α
of␈α∞arguments␈α
it
␈↓ ↓H␈↓must␈α
associate␈α
the␈α∞variables␈α
named␈α
in␈α
the␈α∞variable␈α
list␈α
of␈α
the␈α∞expression␈α
with␈α
the␈α
values␈α∞of␈α
the
␈↓ ↓H␈↓corresponding␈α∞arguments.␈α∞ Since␈α∂this␈α∞evaluation␈α∞may␈α∂occur␈α∞in␈α∞the␈α∞midst␈α∂of␈α∞the␈α∞evaluation␈α∂of␈α∞a
␈↓ ↓H␈↓bigger␈α∞expression␈α∞where␈α∞the␈α∞same␈α∞variable␈α∞names␈α∞occur␈α∞in␈α∞the␈α∞outer␈α∞expression,␈α∞the␈α∞old␈α∞values
␈↓ ↓H␈↓must␈α∀be␈α∃saved␈α∀and␈α∀restored␈α∃when␈α∀the␈α∃current␈α∀evaluation␈α∀is␈α∃completed.␈α∀ Similarly␈α∃when␈α∀a
␈↓ ↓H␈↓␈↓αprogram␈↓ram␈α∂is␈α∂entered␈α∞the␈α∂current␈α∂values␈α∂of␈α∞the␈α∂␈↓αprogram␈↓␈α∂variables␈α∞must␈α∂be␈α∂save␈α∂and␈α∞restore
␈↓ ↓H␈↓136␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓when␈α∩the␈α∪␈↓αprogram␈↓␈α∩is␈α∪exited.␈α∩ (The␈α∪interpreter␈α∩may␈α∪initialize␈α∩these␈α∪variables␈α∩or␈α∪leave␈α∩them
␈↓ ↓H␈↓unbound.)␈α
The␈α
decisions␈α
that␈α
must␈α
be␈α
made␈αinclude:␈α
where␈α
to␈α
keep␈α
the␈α
current␈α
binding,␈α
how␈αto
␈↓ ↓H␈↓save␈α∀and␈α∪restore␈α∀bindings,␈α∀and␈α∪how␈α∀to␈α∀access␈α∪outer␈α∀bindings␈α∀of␈α∪variables.␈α∀ These␈α∀are␈α∪not
␈↓ ↓H␈↓independent.␈α
 For␈αexample␈α
the␈α
current␈αbinding␈α
of␈αa␈α
variable␈α
could␈αbe␈α
kept␈αalways␈α
as␈α
the␈α␈↓¬VALUE
␈↓ ↓H␈↓¬␈↓property.␈α
 When␈α
ever␈α
a␈α
new␈α
binding␈α
is␈α
made␈αthe␈α
old␈α
one␈α
is␈α
saved␈α
on␈α
the␈α
binding␈α
context␈αstack
␈↓ ↓H␈↓and␈α⊂restored␈α⊂when␈α⊂the␈α⊂␈↓αprogram␈↓␈α⊂or␈α⊂λ␈α⊂is␈α⊂exited.␈α⊂ Another␈α⊂possibility␈α⊂is␈α⊂to␈α⊂consider␈α⊂the␈α⊂␈↓¬VALUE
␈↓ ↓H␈↓¬␈↓property␈αas␈αthe␈αtop␈αlevel,␈αglobal␈αenvironment␈αand␈αput␈αnew␈αbindings␈αon␈αthe␈αstack.␈α Thus␈αaccess␈αto
␈↓ ↓H␈↓value is slower, but restoring is faster.

␈↓ ↓H␈↓        For␈α∞expressions␈α∞occuring␈α∞in␈α∞the␈α∞"function"␈α
position␈α∞of␈α∞a␈α∞term,␈α∞the␈α∞interpreter␈α∞must␈α
decide
␈↓ ↓H␈↓what␈αprogram␈αis␈αdenoted.␈α If␈αthe␈αexpression␈α
is␈αa␈αλ-expression␈αthen␈αthe␈αλ-variables␈αare␈α
bound␈αto
␈↓ ↓H␈↓the␈αvalues␈αof␈αthe␈αarguments␈αand␈αthe␈αbody␈αis␈αevaluated.␈α If␈αthe␈αexpression␈αis␈αan␈αatom␈αthere␈αmany
␈↓ ↓H␈↓possible␈α
conventions.␈α
 One␈α
is␈α∞to␈α
always␈α
look␈α
for␈α
an␈α∞applicative␈α
property␈α
(as␈α
discussed␈α∞below)␈α
on
␈↓ ↓H␈↓the␈αproperty␈αlist␈αof␈αthe␈αatom␈αand␈αuse␈αthat␈αvalue.␈α If␈αthere␈αare␈αseveral,␈αthe␈αreasonable␈αchoice␈αis␈αthe
␈↓ ↓H␈↓first␈α
one␈α∞found␈α
as␈α∞it␈α
is␈α∞the␈α
most␈α∞recent␈α
property␈α∞set.␈α
 Another␈α∞possibility␈α
would␈α∞be␈α
to␈α∞check␈α
the
␈↓ ↓H␈↓local␈αenvironment␈αfirst␈αto␈αsee␈αif␈αthe␈αatom␈αis␈αbound␈αto␈αan␈αapplicable␈αexpression.␈α If␈αnone␈αis␈αfound
␈↓ ↓H␈↓then␈α
look␈α∞on␈α
the␈α
property␈α∞list.␈α
 Some␈α
LISPs␈α∞use␈α
the␈α
␈↓¬VALUE␈α∞␈↓property␈α
for␈α
every␈α∞thing␈α
and␈α∞in␈α
the
␈↓ ↓H␈↓case␈αwhere␈αan␈αapplicable␈αobject␈αis␈αneeded,␈αwill␈αrepeat␈αthe␈αevaluation␈α process␈αuntil␈αsuch␈αan␈αobject
␈↓ ↓H␈↓is found, or it can't go further for some reason.

␈↓ ↓H␈↓        The␈α∩problem␈α∩of␈α∩when␈α∩and␈α⊃how␈α∩to␈α∩assign␈α∩values␈α∩to␈α⊃the␈α∩free␈α∩variables␈α∩in␈α∩a␈α⊃function-
␈↓ ↓H␈↓expression␈αis␈α
the␈αsubject␈αof␈α
much␈αcontroversy␈αin␈α
the␈αLISP␈α
world.␈α The␈αquestion␈α
is␈αwhere␈αto␈α
"trap"
␈↓ ↓H␈↓the␈α∃free-variables.␈α∃  It␈α∃is␈α⊗a␈α∃problem␈α∃that␈α∃plagued␈α∃the␈α⊗logicians␈α∃in␈α∃the␈α∃early␈α∃days␈α⊗of␈α∃the
␈↓ ↓H␈↓development␈α
of␈α
formal␈α
logic␈α
and␈α
also␈α
of␈α
λ-calculus.␈α It␈α
arises␈α
when␈α
you␈α
define␈α
what␈α
it␈α
means␈αto
␈↓ ↓H␈↓substitute␈α⊃an␈α⊂expression␈α⊃for␈α⊃occurrences␈α⊂of␈α⊃a␈α⊂bound␈α⊃variable␈α⊃in␈α⊂some␈α⊃other␈α⊃expression.␈α⊂ The
␈↓ ↓H␈↓logicians␈α
solution␈αis␈α
to␈αnot␈α
allow␈α
trapping␈αto␈α
occur.␈α Namely,␈α
outer␈α
bound␈αvariables␈α
are␈αrenamed␈α
if
␈↓ ↓H␈↓they␈α∀occur␈α∀free␈α∃in␈α∀the␈α∀expression␈α∃being␈α∀substituted.␈α∀ Thus␈α∃free␈α∀variables␈α∀remain␈α∃free.␈α∀ In
␈↓ ↓H␈↓computation␈α∩there␈α∩are␈α∩times␈α∩when␈α∩it␈α∩is␈α∩convenient␈α∩to␈α∩allow␈α∩trapping␈α∩of␈α∪free-variables.␈α∩ For
␈↓ ↓H␈↓example␈αthe␈αfunction-expression␈αpassed␈αto␈αa␈αprogram␈αmay␈αjust␈αbe␈αa␈αpiece␈αof␈αcode␈αextracted␈αfrom
␈↓ ↓H␈↓that␈αprogram␈αto␈αmake␈αit␈αeasier␈αto␈αread␈αand␈αyou␈αintend␈αthat␈αthe␈αsame␈αname␈αin␈αboth␈αpieces␈αof␈αcode
␈↓ ↓H␈↓refer␈α
to␈α
the␈α
same␈αthing.␈α
 The␈α
original␈α
LISP␈α
interpreter␈αprovided␈α
maximal␈α
trapping␈α
in␈α
the␈αsense
␈↓ ↓H␈↓that␈α⊂variables␈α∂function-expressions␈α⊂were␈α∂always␈α⊂assumed␈α⊂to␈α∂refer␈α⊂the␈α∂the␈α⊂most␈α⊂recent␈α∂binding.
␈↓ ↓H␈↓(This␈α∞was␈α∞more␈α
of␈α∞an␈α∞oversight␈α∞than␈α
a␈α∞concious␈α∞decision.)␈α
Current␈α∞LISPs␈α∞usually␈α∞provide␈α
both
␈↓ ↓H␈↓alternatives to some degree.  In MACLISP  evaluating

␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓¬(FUNCTION (LAMBDA (X) (PLUS X Y)))␈↓↓␈↓ 

␈↓ ↓H␈↓produces the undecorated λ-expression

␈↓ ↓H␈↓␈↓ ¬⊂␈↓↓␈↓¬(LAMBDA (X) (PLUS X Y))␈↓↓␈↓ 

␈↓ ↓H␈↓which when applied will use the value of ␈↓¬Y ␈↓at the time of application,  while evaluating

␈↓ ↓H␈↓␈↓ ∧0␈↓↓␈↓¬(*FUNCTION (LAMBDA (X) (PLUS X Y)))␈↓↓␈↓ 

␈↓ ↓H␈↓produces the "funarg" term

␈↓ ↓H␈↓␈↓ βX␈↓↓␈↓¬(FUNARG (LAMBDA (X) (PLUS X Y)) <current-bcp>)␈↓↓␈↓ 

␈↓ ↓H␈↓where␈α∂␈↓¬<current-bcp>␈α∂␈↓is␈α∂a␈α∂pointer␈α∂to␈α∂the␈α∂current␈α∂binding␈α∂context.␈α∂ When␈α∂applied␈α⊂the␈α∂context
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠137


␈↓ ↓H␈↓indicted␈α⊃by␈α∩the␈α⊃3rd␈α∩element␈α⊃of␈α⊃a␈α∩funarg␈α⊃is␈α∩used␈α⊃instead␈α⊃of␈α∩the␈α⊃binding␈α∩context␈α⊃in␈α∩force␈α⊃at
␈↓ ↓H␈↓application␈α⊂time.␈α⊃  There␈α⊂are␈α⊃many␈α⊂subtilties␈α⊂involved␈α⊃in␈α⊂solving␈α⊃the␈α⊂"funarg"␈α⊃problem.␈α⊂  For
␈↓ ↓H␈↓example,␈α∃what␈α∃if␈α∃the␈α∃binding␈α∃context␈α∃referred␈α∃to␈α∃in␈α∃a␈α∃MACLISP␈α∃funarg␈α⊗expression␈α∃has
␈↓ ↓H␈↓disappeared␈αbefore␈αit␈αis␈αused?␈α This␈αis␈αa␈α"funval"␈αproblem␈αin␈αthe␈αsense␈αthat␈αit␈αis␈αwhen␈αthe␈αfunarg
␈↓ ↓H␈↓expression␈αis␈αpassed␈αup␈αas␈αa␈αvalue␈αof␈αa␈αcomputation␈αto␈αbe␈αused␈αlater␈αthe␈αbinding␈αcontext␈αis␈αlikely
␈↓ ↓H␈↓to have disappeared (popped from the stack).  We won't pursue the problem further.

␈↓ ↓H␈↓[References?]

␈↓ ↓H␈↓        We␈αconclude␈α
the␈αdiscussion␈α
of␈αevaluation␈α
with␈αa␈α
brief␈αdescription␈α
of␈αthe␈α
use␈αof␈α
some␈αof␈α
 the
␈↓ ↓H␈↓standard␈α⊂system␈α⊂propertys␈α∂other␈α⊂that␈α⊂␈↓¬PNAME␈α⊂␈↓and␈α∂␈↓¬VALUE.␈α⊂␈↓␈α⊂An␈α⊂atom␈α∂with␈α⊂an␈α⊂␈↓¬EXPR␈α⊂␈↓property␈α∂is
␈↓ ↓H␈↓assumed␈αby␈αLISP␈αto␈αdenote␈αan␈αinterpretable␈αprogram␈αand␈αthe␈αvalue␈αof␈αthe␈α␈↓¬EXPR␈α␈↓property␈αshould
␈↓ ↓H␈↓be␈α
an␈α
application␈αterm␈α
(such␈α
as␈α
a␈αprogram␈α
name,␈α
a␈αλ-expression,␈α
or␈α
a␈α
label-expression)␈αsuitable
␈↓ ↓H␈↓for␈αbeing␈αinterpreted.␈α Recall␈αthat␈αthis␈αproperty␈αcan␈αbe␈αset␈α using␈αone␈αof␈αthe␈αoperations␈α␈↓¬DEFUN␈α␈↓or
␈↓ ↓H␈↓␈↓¬DEFPROP␈α∞␈↓␈α
described␈α∞in␈α∞Chapter␈α
I.␈α∞ An␈α
atom␈α∞with␈α∞a␈α
␈↓¬SUBR␈α∞␈↓property␈α
denotes␈α∞a␈α∞compiled␈α
program
␈↓ ↓H␈↓which␈α
may␈α
be␈α
directly␈α
executed.␈α
 The␈α
value␈αof␈α
the␈α
␈↓¬SUBR␈α
␈↓property␈α
is␈α
a␈α
subroutine␈α
pointer␈αwhich
␈↓ ↓H␈↓points␈α
to␈α∞the␈α
compiled␈α
program.␈α∞ Atoms␈α
denoting␈α
built␈α∞in␈α
program␈α
such␈α∞as␈α
␈↓¬REVERSE␈α∞␈↓or␈α
␈↓¬APPEND
␈↓ ↓H␈↓¬␈↓have␈αa␈α␈↓¬SUBR␈α␈↓property␈αautomatically.␈α When␈αa␈αcompiled␈αprogram␈αis␈αloaded␈αinto␈αLISP␈αthe␈αname␈αof
␈↓ ↓H␈↓that␈α∩program␈α∩gets␈α∩a␈α∩␈↓¬SUBR␈α∩␈↓property.␈α∩ The␈α∩properties␈α∩␈↓¬FEXPR␈α∩␈↓and␈α∩␈↓¬LEXPR␈α∩are␈α∩␈↓similar␈α∩to␈α∩␈↓¬EXPR
␈↓ ↓H␈↓¬␈↓except␈α⊃that␈α⊃the␈α⊃arguments␈α∩to␈α⊃the␈α⊃program␈α⊃are␈α⊃treated␈α∩differently.␈α⊃ In␈α⊃the␈α⊃␈↓¬EXPR␈α⊃␈↓case␈α∩a␈α⊃fixed
␈↓ ↓H␈↓number␈αof␈αarguments␈αare␈αexpected␈αand␈αthey␈αare␈αevaluated␈αbefore␈αexecuting␈αthe␈αprogram.␈α In␈αthe
␈↓ ↓H␈↓␈↓¬FEXPR␈α
␈↓case␈α
the␈α
list␈α
of␈α
arguments␈α
(eg.␈α∞the␈α
d-part␈α
of␈α
the␈α
expression)␈α
is␈α
passed␈α
unevaluated␈α∞to␈α
the
␈↓ ↓H␈↓program␈α∪as␈α∪a␈α∩single␈α∪entity.␈α∪ In␈α∩the␈α∪␈↓¬LEXPR␈α∪␈↓case␈α∩the␈α∪number␈α∪of␈α∩arguments␈α∪is␈α∪arbitrary,␈α∩ the
␈↓ ↓H␈↓arguments␈αare␈αevaluated␈αand␈αsaved␈α(typically␈αon␈αthe␈αstack),␈αthe␈αnumber␈αof␈αarguments␈αis␈αpassed␈αto
␈↓ ↓H␈↓the␈α
program␈αand␈α
the␈αprogram␈α
must␈αuse␈α
a␈αspecial␈α
auxiliary␈αprogram␈α
␈↓↓args␈↓␈αto␈α
access␈α
the␈αargument
␈↓ ↓H␈↓values.␈α
 The␈α
three␈α∞␈↓¬SUBR␈α
␈↓cases␈α
are␈α
analogous␈α∞to␈α
those␈α
for␈α
␈↓¬EXPR.␈α∞␈↓␈α
We␈α
can␈α
express␈α∞the␈α
difference
␈↓ ↓H␈↓between␈α∪␈↓¬DEFPROP␈α∪␈↓and␈α∪␈↓¬PUTPROP␈α∪␈↓explained␈α∪above␈α∩by␈α∪saying␈α∪that␈α∪␈↓¬DEFPROP␈α∪␈↓is␈α∪an␈α∪␈↓¬FSUBR␈α∩␈↓and
␈↓ ↓H␈↓␈↓¬PUTPROP ␈↓is a ␈↓¬SUBR. ␈↓

␈↓ ↓H␈↓        If␈α
an␈α
atom␈α
has␈α
a␈α
␈↓¬MACRO␈α
␈↓property␈α
then␈α
that␈α
property␈α
should␈α
be␈α
a␈α
 application␈α
term␈α
taking
␈↓ ↓H␈↓one␈α∀argument.␈α∀ When␈α∀such␈α∀an␈α∀atom␈α∀is␈α∪encountered␈α∀as␈α∀the␈α∀a-part␈α∀of␈α∀an␈α∀expression␈α∪being
␈↓ ↓H␈↓evaluated,␈α∂the␈α∂corresponding␈α∞macro␈α∂definition␈α∂is␈α∞applied␈α∂to␈α∂the␈α∞entire␈α∂expression,␈α∂as␈α∂the␈α∞single
␈↓ ↓H␈↓argument,␈α
and␈α
the␈α
value␈α
is␈αa␈α
new␈α
expression␈α
that␈α
is␈α
evaluated␈αin␈α
place␈α
of␈α
the␈α
original␈α
one.␈α An
␈↓ ↓H␈↓atom␈α∞can␈α
be␈α∞given␈α
the␈α∞␈↓¬MACRO␈α
␈↓property␈α∞using␈α
␈↓¬DEFUN␈α∞␈↓with␈α
type␈α∞␈↓¬MACRO␈α
specified␈α∞␈↓or␈α∞some␈α
other
␈↓ ↓H␈↓macro-definition␈α∞facility␈α
provided␈α∞by␈α
the␈α∞system␈α
or␈α∞defined␈α
by␈α∞the␈α
user.␈α∞ See␈α
Chapter␈α∞V␈α∞␈↓π∞␈↓3␈α
for
␈↓ ↓H␈↓more details.

␈↓ ↓H␈↓        An␈αatom␈αwith␈αan␈α␈↓¬ARRAY␈α␈↓property␈αdenotes␈αan␈αarray.␈α The␈αvalue␈αof␈αthe␈α␈↓¬ARRAY␈α␈↓property␈αis␈αan
␈↓ ↓H␈↓array␈αpointer␈α
which␈αprovides␈αLISP␈α
with␈αa␈α
means␈αof␈αaccessing␈α
the␈αarray.␈α
 The␈α␈↓¬ARRAY␈α␈↓property␈α
can
␈↓ ↓H␈↓be␈αset␈αusing␈αthe␈α␈↓¬ARRAY␈α␈↓command␈αas␈αexplained␈αin␈αChapter␈αV␈α␈↓π∞␈↓2.␈α The␈αarray␈αpointer␈αincludes␈αtype,
␈↓ ↓H␈↓bounds, and location information.

␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓1.␈α Modify␈α␈↓↓read␈↓␈α
and␈α␈↓↓reada␈↓␈αto␈α
do␈αsome␈αsyntax␈αchecking␈α
and␈αreturn␈α"error␈α
messages"␈αis␈αcase␈α
of␈αan
␈↓ ↓H␈↓ill-formed argument.

␈↓ ↓H␈↓2. Rewrite ␈↓↓readdot␈↓ as a sequential program with out any recursive calls.
␈↓ ↓H␈↓138␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓3.  ␈↓αEditing LISP programs.␈↓


␈↓ ↓H␈↓        An␈αimportant␈αpart␈αof␈αany␈αLISP␈αsystem␈αis␈αan␈αinteracitve␈αeditor.␈α With␈αthe␈αhelp␈αof␈αthe␈α
editor
␈↓ ↓H␈↓you␈α∂can␈α∂write␈α∂and␈α∞modify␈α∂programs.␈α∂ Some␈α∂editors␈α∂allow␈α∞you␈α∂to␈α∂evaluate␈α∂expressions␈α∂with␈α∞out
␈↓ ↓H␈↓leaving␈α
the␈αediting␈α
environment.␈α They␈α
may␈αalso␈α
provide␈αfacility␈α
for␈αediting␈α
S-expressions␈αother
␈↓ ↓H␈↓than␈αprograms.␈α The␈αfact␈αthat␈αLISP␈αprograms␈αare␈αS-expressions␈αwith␈αa␈αvery␈αsimple␈αsyntax␈α
means
␈↓ ↓H␈↓that it is easy to write a simple but powerful LISP program editor in LISP.

␈↓ ↓H␈↓        In␈α∞this␈α
section␈α∞we␈α∞will␈α
present␈α∞a␈α∞simple␈α
interactive␈α∞editor␈α∞based␈α
on␈α∞the␈α∞MACLISP␈α
editor.
␈↓ ↓H␈↓Before␈α∂describing␈α∂this␈α∂program␈α∂let␈α∂us␈α∂consider␈α⊂what␈α∂we␈α∂want␈α∂from␈α∂a␈α∂program␈α∂editor.␈α⊂ First␈α∂it
␈↓ ↓H␈↓should␈α∂be␈α⊂able␈α∂to␈α⊂lookup␈α∂the␈α⊂current␈α∂program␈α⊂given␈α∂its␈α∂name␈α⊂and␈α∂be␈α⊂able␈α∂to␈α⊂replace␈α∂it␈α⊂by␈α∂a
␈↓ ↓H␈↓modified␈α∞version.␈α∞ Second␈α∞it␈α∞should␈α∞be␈α∞able␈α∞to␈α∞move␈α∞around␈α∞in␈α∞the␈α∞program␈α∞being␈α∂edited␈α∞thus
␈↓ ↓H␈↓changing␈αits␈αnotion␈αof␈αthe␈α
␈↓↓current␈↓␈α␈↓↓expression␈↓␈α(known␈αas␈αthe␈α␈↓¬CE␈↓).␈α
 Third␈αit␈αshould␈αbe␈αable␈αto␈α
insert
␈↓ ↓H␈↓and␈α⊃delete␈α⊃sub-expressions␈α⊃(elements␈α⊃of␈α⊃the␈α∩␈↓¬CE␈↓).␈α⊃ This␈α⊃is␈α⊃sufficient␈α⊃capability␈α⊃to␈α∩convert␈α⊃any
␈↓ ↓H␈↓program␈αinto␈αany␈α
other.␈α It␈αis␈α
also␈αsomewhat␈αessential␈α
to␈αbe␈αable␈α
to␈αlook␈αat␈α
the␈α␈↓¬CE,␈α␈↓thus␈αthe␈α
editor
␈↓ ↓H␈↓should␈αbe␈αable␈αto␈αprint␈αthe␈α␈↓¬CE.␈α␈↓␈αOther␈αfeatures␈αwhich␈αare␈αuseful␈αinclude␈αmoving␈αparentheses␈α"in
␈↓ ↓H␈↓and␈α∞out",␈α∞wrapping␈α∞a␈α∞subsequence␈α∞of␈α∞a␈α∞list␈α∞into␈α
a␈α∞list␈α∞--␈α∞e.g.␈α∞inserting␈α∞a␈α∞par␈α∞of␈α∞parentheses,␈α
or
␈↓ ↓H␈↓unwrapping␈αan␈αelement␈αof␈αa␈αlist␈αand␈αsplicing␈αits␈αelements␈αinto␈αthe␈αcontaining␈αlist␈α--␈αe.g.␈αerasing␈αa
␈↓ ↓H␈↓pair␈αof␈αparentheses,␈αfinding␈αthe␈αfirst␈αoccurrence␈αof␈αsome␈αexpression␈α--␈αe.g.␈αchangeing␈αthe␈α␈↓¬CE␈α␈↓to␈α
be
␈↓ ↓H␈↓that␈α⊃occurrence␈α∩or␈α⊃maybe␈α∩the␈α⊃list␈α∩containing␈α⊃that␈α∩occurrence,␈α⊃replacing␈α∩all␈α⊃occurrences␈α∩of␈α⊃an
␈↓ ↓H␈↓expression␈αby␈α
another␈αexpression␈α--␈α
either␈αat␈αthe␈α
top␈αlevel␈αof␈α
the␈α␈↓¬CE␈α␈↓or␈α
at␈αall␈α
levels,␈αtransposing
␈↓ ↓H␈↓members␈αof␈αthe␈α␈↓¬CE,␈α␈↓moving␈αan␈αexpression␈αfrom␈αon␈αplace␈αto␈αanother,␈αor␈αreversing␈αthe␈α␈↓¬CE.␈α␈↓␈αThese
␈↓ ↓H␈↓are␈α∂only␈α∂a␈α∞few␈α∂ideas.␈α∂ We␈α∞implement␈α∂a␈α∂representative␈α∞selection.␈α∂ The␈α∂reader␈α∞should␈α∂be␈α∂able␈α∞to
␈↓ ↓H␈↓implement␈α⊗any␈α⊗others␈α⊗without␈α⊗much␈α∃difficulty.␈α⊗ Some␈α⊗editors␈α⊗also␈α⊗have␈α⊗file␈α∃manipulation
␈↓ ↓H␈↓capability,␈α
so␈α
old␈α
definitions␈α
can␈α
be␈α
looked␈α
up␈α
in␈α
files␈α
and␈α
new␈α
ones␈α
saved␈α
or␈α
used␈α
to␈α∞update␈α
a
␈↓ ↓H␈↓file.   We will not treat this aspect of editing.

␈↓ ↓H␈↓        The␈α
program␈α
␈↓↓editor␈↓␈α
takes␈α
program␈α
name␈αas␈α
an␈α
argument.␈α
 When␈α
invoked,␈α
␈↓↓editor␈↓␈α
looks␈αup
␈↓ ↓H␈↓the␈αprogram␈α
by␈αgetting␈α
the␈α␈↓¬EXPR␈α␈↓property,␈α
initializes␈αthe␈α
global␈αparameters␈α␈↓↓fn␈↓␈α
␈↓↓top,␈↓␈α␈↓↓ce␈↓␈α
and␈α␈↓↓chain␈↓
␈↓ ↓H␈↓and␈α
then␈α
goes␈α
into␈α∞a␈α
command␈α
reading␈α
-␈α∞executing␈α
loop.␈α
 The␈α
parameter␈α∞␈↓↓fn␈↓␈α
is␈α
just␈α
to␈α∞save␈α
the
␈↓ ↓H␈↓program␈α
name.␈α
 ␈↓↓top␈↓␈α
is␈α
the␈α
program␈α
being␈α
edited.␈α
␈↓↓ce␈↓␈α
the␈α
current␈α
expression␈α
(␈↓↓top␈↓␈α
initially).␈α
 ␈↓↓chain␈↓␈α
the
␈↓ ↓H␈↓chain␈αof␈α
expressions␈αleading␈αfrom␈α
␈↓↓top␈↓␈αto␈α␈↓↓ce.␈α
If␈↓␈α␈↓↓top=ce␈↓␈α
the␈α␈↓↓chain=␈↓¬NIL␈↓↓␈↓␈αotherwise␈α
the␈αfirst␈αelement␈α
of
␈↓ ↓H␈↓␈↓↓chain␈↓␈α
is␈α
a␈α
pair␈α
␈↓↓[n . e]␈↓␈α
where␈α
␈↓↓e␈↓␈α
is␈α
the␈α
expression␈α
immediately␈α
containing␈α
␈↓↓ce␈↓␈α
and␈α
␈↓↓n␈↓␈α
is␈α
the␈αposition␈α
 of
␈↓ ↓H␈↓␈↓↓ce␈↓ in ␈↓↓e.␈↓

␈↓ ↓H␈↓        There␈αare␈αtwo␈α
kinds␈αof␈αcommands␈αthose␈α
handled␈αdirectly␈αby␈αthe␈α
editor␈αand␈αthose␈αwhich␈α
the
␈↓ ↓H␈↓editor␈α∞uses␈α
to␈α∞look␈α
up␈α∞what␈α
should␈α∞be␈α∞done.␈α
 The␈α∞direct␈α
commands␈α∞are␈α
␈↓¬Q,␈α∞␈↓␈↓¬OK,␈α
␈↓or␈α∞␈↓¬<n>␈α∞␈↓for␈α
any
␈↓ ↓H␈↓number␈α∂␈↓¬n.␈α∂␈↓␈α⊂␈↓¬Q␈α∂␈↓means␈α∂exit␈α⊂the␈α∂editor,␈α∂␈↓¬OK␈α∂␈↓means␈α⊂replace␈α∂the␈α∂old␈α⊂version␈α∂of␈α∂the␈α⊂program␈α∂being
␈↓ ↓H␈↓edited␈α∂by␈α∂the␈α∂new␈α∞one␈α∂and␈α∂then␈α∂exit.␈α∂ ␈↓¬<n>␈α∞␈↓mean␈α∂replace␈α∂the␈α∂␈↓↓ce␈↓␈α∂by␈α∞the␈α∂nth␈α∂element␈α∂of␈α∂the␈α∞␈↓↓ce.␈↓
␈↓ ↓H␈↓There␈αare␈αtwo␈αtype␈αof␈αlookup␈αcommands,␈αatomic␈αand␈αlist.␈α For␈αatomic␈αcommands␈αthe␈αeditor␈αlooks
␈↓ ↓H␈↓up␈αthe␈αprogram␈αto␈αexecute␈α
on␈αthe␈α␈↓¬ATOMIC-EDIT-FNS␈α␈↓property␈αof␈α
that␈αatom␈αand␈αruns␈αit.␈α
 For␈αlist
␈↓ ↓H␈↓commands␈α∪the␈α∪editor␈α∪looks␈α∪up␈α∪the␈α∪program␈α∪to␈α∪execute␈α∪on␈α∪the␈α∪␈↓¬LIST-EDIT-FNS␈α∀␈↓property␈α∪of
␈↓ ↓H␈↓␈↓↓␈↓αa|␈↓↓command␈↓␈α⊂and␈α⊂applies␈α⊂it␈α⊂to␈α⊂␈↓↓␈↓αd|␈↓↓command␈↓.␈α⊂ If␈α⊂in␈α⊂either␈α⊂case␈α⊂the␈α⊂property␈α⊂is␈α⊂not␈α⊂found␈α⊃then␈α⊂the
␈↓ ↓H␈↓command␈αis␈αevaluated␈αas␈αan␈αordinary␈αterm␈αand␈αthe␈αresult␈αprinted.␈α This␈αstyle␈αof␈αprogramming␈αis
␈↓ ↓H␈↓sometimes␈α
called␈α
"data␈α
directed".␈α
 It␈α
makes␈α
the␈α
editor␈α
extremely␈α
easy␈α
to␈α
extend␈α
or␈α
modify.␈α Since
␈↓ ↓H␈↓the␈αdata␈αstructures␈α
manipulated␈αare␈αquite␈α
simple␈αand␈αeasily␈α
explained,␈αit␈αcan␈α
even␈αbe␈αexplained␈α
to
␈↓ ↓H␈↓a␈α
user␈α
in␈α
sufficient␈α
detail␈α
that␈α
the␈α
user␈α
can␈α
implement␈α
his␈α
own␈α
favorite␈α
commands.␈α
 The␈αreason
␈↓ ↓H␈↓that␈α∞the␈α∞numeric␈α
commands␈α∞are␈α∞treated␈α∞directly␈α
is␈α∞that␈α∞numbers␈α
don't␈α∞have␈α∞property␈α∞lists.␈α
 The
␈↓ ↓H␈↓editor␈αcould␈αbe␈α
made␈αmore␈αuniform␈αby␈α
giving␈αthese␈αcommands␈α
symbolic␈αnames,␈αbut␈αmoving␈α
down
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠139


␈↓ ↓H␈↓into␈α
an␈α
expression␈α
via␈α
such␈α
commands␈α
is␈α
sufficiently␈α
convenient␈α
to␈α
make␈α
it␈α
work␈α
implementing␈α
the
␈↓ ↓H␈↓special␈αcase.␈α
 We␈αtake␈α
some␈αcare␈α
in␈αthe␈α
editor␈αand␈α
command␈αcodes␈α
to␈αmake␈α
sure␈αthe␈αcommand␈α
will
␈↓ ↓H␈↓work␈α
in␈α
the␈α
context␈α
it␈α
has␈αbeen␈α
given.␈α
 If␈α
not␈α
an␈α
error␈αmessage␈α
is␈α
printed␈α
and␈α
no␈α
action␈αoccurs.
␈↓ ↓H␈↓Many␈αof␈αthe␈αcommands␈α
work␈αdestructively.␈α This␈αis␈α
safe␈αbecause␈αthe␈αexpression␈α
to␈αedit␈αis␈αa␈α
"copy"
␈↓ ↓H␈↓of␈α⊂the␈α⊂original␈α⊃definition.␈α⊂  In␈α⊂fact␈α⊃one␈α⊂might␈α⊂consider␈α⊂it␈α⊃essential␈α⊂to␈α⊂edit␈α⊃destrictively.␈α⊂ Since
␈↓ ↓H␈↓otherwise,␈α
after␈αeach␈α
change␈αthe␈α
expression␈αwould␈α
have␈αto␈α
be␈α
rebuilt.␈α At␈α
least␈αit␈α
is␈αin␈α
the␈αspirit␈α
of
␈↓ ↓H␈↓editing to make destructive changes, since the point of editing in to change the expression.

␈↓ ↓H␈↓The program ␈↓↓editor␈↓ and its auxiliary functions are defined as follows.

␈↓ ↓H␈↓␈↓ α8␈↓↓        editor l (FEXPR) ← ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓            ␈↓αprogram␈↓↓ [fn, top, ce, chain, command, efn]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 fn ← ␈↓αa|␈↓↓l␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 top ← copy get[fn, ␈↓¬EXPR␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αif␈↓↓ ␈↓αn|␈↓↓top ␈↓αthen␈↓↓ [errmsg0[]; ␈↓αreturn␈↓↓ ␈↓¬NO-EDIT␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ce ← top; chain ← ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓         ␈↓¬EDLOOP:␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 print ␈↓¬ε␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 command ← read[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αif␈↓↓ command = ␈↓¬Q␈↓↓ ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ ␈↓¬BYE␈↓↓␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ command = ␈↓¬OK␈↓↓ ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ putprop[fn, top, ␈↓¬EXPR␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αelse␈↓↓ ␈↓αif␈↓↓ numberp command ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓3.1)␈↓ α8␈↓↓                       [␈↓αif␈↓↓ ␈↓αat|␈↓↓ce ∨ command > length ce ␈↓αthen␈↓↓ [errmsg1[], ␈↓αgo to␈↓↓ ␈↓¬EDLOOP␈↓↓], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                        chain ← [command . ce] . chain, ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                        ce ← nth[ce, command], ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                        ␈↓αgo to␈↓↓ ␈↓¬EDLOOP␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αif␈↓↓ ␈↓αat|␈↓↓command ␈↓αthen␈↓↓ efn ← get[command, ␈↓¬ATOMIC-EDIT-FN␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                   ␈↓αelse␈↓↓ efn ← get[␈↓αa|␈↓↓command, ␈↓¬LIST-EDIT-FN␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αif␈↓↓ efn = ␈↓¬NIL␈↓↓ ␈↓αthen␈↓↓ [print eval command, ␈↓αgo to␈↓↓ ␈↓¬EDLOOP␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                   ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ␈↓αat|␈↓↓command ␈↓αthen␈↓↓ [eval efn, ␈↓αgo to␈↓↓ ␈↓¬EDLOOP␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                   ␈↓αelse␈↓↓ ␈↓αif␈↓↓ ¬␈↓αat|␈↓↓command ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                       [apply[efn, ␈↓αd|␈↓↓command], ␈↓αgo to␈↓↓ ␈↓¬EDLOOP␈↓↓]␈↓


␈↓ ↓H␈↓␈↓ α8␈↓↓        errmsg0[] ← [print fn, princ ␈↓¬`not an EXPR'␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        errmsg1[] ← [terpri[], princ ␈↓¬`> length of CE'␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        errmsg2[] ← [terpri[], princ ␈↓¬`Unknown command'␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        errmsg3[] ← [terpri[], princ ␈↓¬`You are at the top'␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        errmsg4[] ← [terpri[], princ ␈↓¬`You are at the left edge'␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        errmsg5[] ← [terpri[], princ ␈↓¬`You are at the right edge'␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓        errmsg6[] ← [terpri[], princ ␈↓¬`CE is atomic'␈↓↓]␈↓

␈↓ ↓H␈↓3.2)␈↓ α8␈↓↓        nth[u, n] ← ␈↓αif␈↓↓ n > 1 ∧ ¬␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ nth[␈↓αd|␈↓↓u, sub1 n] ␈↓αelse␈↓↓ ␈↓αa|␈↓↓u␈↓

␈↓ ↓H␈↓3.3)␈↓ α8␈↓↓        pos[u, n] ← ␈↓αif␈↓↓ n > 1 ∧ ¬␈↓αn|␈↓↓␈↓αd|␈↓↓u ␈↓αthen␈↓↓ pos[␈↓αd|␈↓↓u, sub1 n] ␈↓αelse␈↓↓ u␈↓

␈↓ ↓H␈↓3.4)␈↓ α8␈↓↓        copy x ← ␈↓αif␈↓↓ ␈↓αat|␈↓↓x ␈↓αthen␈↓↓ x ␈↓αelse␈↓↓ copy ␈↓αa|␈↓↓x . copy ␈↓αd|␈↓↓x␈↓


␈↓ ↓H␈↓        We have implemented the following atomic commands:
␈↓ ↓H␈↓␈↓¬P ␈↓; print the ␈↓↓ce␈↓,
␈↓ ↓H␈↓140␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓␈↓¬UP ␈↓; move up one level in the chain,
␈↓ ↓H␈↓␈↓¬RT ␈↓; move one to the right,
␈↓ ↓H␈↓␈↓¬LI ␈↓; move the left parenthesis in one
␈↓ ↓H␈↓        thus removing the first element of the ␈↓↓ce␈↓ and inserting it before the ␈↓↓ce␈↓),  and
␈↓ ↓H␈↓␈↓¬RO ␈↓; move the right parenthesis out one
␈↓ ↓H␈↓        thus making the right sibling of the ␈↓↓ce␈↓ the last element of the ␈↓↓ce␈↓.


␈↓ ↓H␈↓The␈α∞expressions␈α∞put␈α∞on␈α
the␈α∞␈↓¬ATOMIC-EDIT-FN␈α∞␈↓properties␈α∞are␈α
shown␈α∞below.␈α∞ If␈α∞you␈α∞have␈α
trouble
␈↓ ↓H␈↓figuring␈α⊂out␈α∂how␈α⊂some␈α∂of␈α⊂the␈α∂pointer␈α⊂manipulations␈α∂work␈α⊂we␈α∂suggest␈α⊂drawing␈α∂a␈α⊂list␈α∂structure
␈↓ ↓H␈↓(boxes and pointers) of some ␈↓¬CE ␈↓and following through the sequence of assignments.

␈↓ ↓H␈↓3.5)␈↓ αλ␈↓↓    p:␈↓¬ATOMIC-EDIT-FN␈↓↓ ← print ce␈↓␈↓ πx;print the ␈↓↓ce␈↓

␈↓ ↓H␈↓␈↓ αλ␈↓↓    up:␈↓¬ATOMIC-EDIT-FN␈↓↓ ← ␈↓αprogram␈↓↓ []␈↓␈↓ πx;␈↓↓ce␈↓ ← parent expression
␈↓ ↓H␈↓3.6)␈↓ αλ␈↓↓                    ␈↓αif␈↓↓ ␈↓αn|␈↓↓chain ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg3[]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ce ← ␈↓αda|␈↓↓chain␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    chain ← ␈↓αd|␈↓↓chain␈↓

␈↓ ↓H␈↓␈↓ αλ␈↓↓    rt:␈↓¬ATOMIC-EDIT-FN␈↓↓ ← ␈↓αprogram␈↓↓ [n]␈↓␈↓ πx;␈↓↓ce␈↓ ← expression to right
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ␈↓αif␈↓↓ ␈↓αn|␈↓↓chain ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg3[]␈↓
␈↓ ↓H␈↓3.7)␈↓ αλ␈↓↓                    n ← add1 ␈↓αaa|␈↓↓chain␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ␈↓αif␈↓↓ n > length ␈↓αda|␈↓↓chain ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg5[]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ce ← nth[␈↓αda|␈↓↓chain, n]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ␈↓αaa|␈↓↓chain ← n␈↓

␈↓ ↓H␈↓␈↓ αλ␈↓↓    li:␈↓¬ATOMIC-EDIT-FN␈↓↓ ← ␈↓αprogram␈↓↓ [cetmp, pos]␈↓␈↓ πx;move left paren in
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ␈↓αif␈↓↓ ␈↓αn|␈↓↓chain ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg3[]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ␈↓αif␈↓↓ ␈↓αat|␈↓↓ce ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg6[]␈↓
␈↓ ↓H␈↓3.8)␈↓ αλ␈↓↓                    pos ← pos[␈↓αda|␈↓↓chain, ␈↓αaa|␈↓↓chain]␈↓␈↓ πx;point to ␈↓↓ce␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ␈↓αd|␈↓↓pos ← ␈↓αd|␈↓↓ce . ␈↓αd|␈↓↓pos␈↓␈↓ πx;insert ␈↓↓␈↓αd|␈↓↓ce␈↓ after ␈↓↓ce␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ␈↓αa|␈↓↓pos ← ␈↓αa|␈↓↓ce␈↓␈↓ πx;replace ␈↓↓ce␈↓ by ␈↓↓␈↓αa|␈↓↓ce␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ce ← ␈↓αd|␈↓↓ce␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ␈↓αaa|␈↓↓chain ← add1 ␈↓αaa|␈↓↓chain␈↓

␈↓ ↓H␈↓␈↓ αλ␈↓↓    ro:␈↓¬ATOMIC-EDIT-FN␈↓↓ ← ␈↓αprogram␈↓↓ [cetmp, pos, pos1]␈↓␈↓ πx;move right paren out
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ␈↓αif␈↓↓ ␈↓αn|␈↓↓chain ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg3[]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ␈↓αif␈↓↓ ␈↓αat|␈↓↓ce ∧ ¬␈↓αn|␈↓↓ce ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg6[]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    pos ← pos[␈↓αda|␈↓↓chain, ␈↓αaa|␈↓↓chain]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    pos1 ← ␈↓αd|␈↓↓pos␈↓
␈↓ ↓H␈↓3.9)␈↓ αλ␈↓↓                    ␈↓αif␈↓↓ ␈↓αn|␈↓↓pos1 ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg5[]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    cetmp ← ␈↓¬NIL␈↓↓ . ce␈↓␈↓ πx;in case ␈↓↓ce␈↓ is ␈↓¬NIL␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    nconc[cetmp, pos1]␈↓␈↓ πx;add next element to end of ␈↓↓ce␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ␈↓αd|␈↓↓pos ← ␈↓αd|␈↓↓pos1␈↓␈↓ πx;delete it from parent list
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ␈↓αd|␈↓↓pos1 ← ␈↓¬NIL␈↓↓␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ce ← ␈↓αd|␈↓↓cetmp␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓↓                    ␈↓αa|␈↓↓pos ← ce ␈↓␈↓ πx;make sure new ␈↓↓ce␈↓ is in parent list

␈↓ ↓H␈↓        We have implemented the following list commands:
␈↓ ↓H␈↓(␈↓¬I ␈↓<n> <exp>)   ; insert <exp> before the <n>th element of the ␈↓↓ce,␈↓
␈↓ ↓H␈↓(␈↓¬D ␈↓<n>) ; delete the <n>th element of the ␈↓↓ce.␈↓
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠141


␈↓ ↓H␈↓The programs for executing these commands are given by

␈↓ ↓H␈↓␈↓ α8␈↓↓    i:␈↓¬LIST-EDIT-FN␈↓↓ ← ␈↓␈↓ πx;insert ␈↓↓x␈↓ before the ␈↓↓n␈↓th element
␈↓ ↓H␈↓␈↓ α8␈↓↓        λn x.[␈↓αprogram␈↓↓ [pos, tmp]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αif␈↓↓ n < 0 ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg2[]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                   ␈↓αelse␈↓↓ ␈↓αif␈↓↓ n = 1 ␈↓αthen␈↓↓ ␈↓
␈↓ ↓H␈↓3.10)␈↓ α8␈↓↓                        ␈↓αif␈↓↓ ␈↓αn|␈↓↓chain ␈↓αthen␈↓↓ top ← [ce ← x . ce]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                        ␈↓αelse␈↓↓ [ce ← x . ce ; ␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                               ␈↓αa|␈↓↓pos[␈↓αda|␈↓↓chain, ␈↓αaa|␈↓↓chain] ← ce]␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 pos ← pos[ce, sub1 n]␈↓␈↓ πx;point to position for insertion
␈↓ ↓H␈↓␈↓ α8␈↓↓                 tmp ← x . ␈↓¬NIL␈↓↓␈↓␈↓ πx;make list containing ␈↓↓x␈↓
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αd|␈↓↓tmp ← ␈↓αd|␈↓↓pos ␈↓␈↓ πx;splice it in
␈↓ ↓H␈↓␈↓ α8␈↓↓                 ␈↓αd|␈↓↓pos ← tmp ] ␈↓

␈↓ ↓H␈↓␈↓ α8␈↓↓    d:␈↓¬LIST-EDIT-FN␈↓↓ ←␈↓ πx; delete the ␈↓␈↓↓nth element of the ce
␈↓ ↓H␈↓↓␈↓ α8            λn.[␈↓αprogram␈↓↓ [pos, tmp]
␈↓ ↓H␈↓↓␈↓ α8                   ␈↓αif␈↓↓ n < 0 ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg2[]
␈↓ ↓H␈↓↓␈↓ α8                     ␈↓αelse␈↓↓ ␈↓αif␈↓↓ n = 1 ␈↓αthen␈↓↓ 
␈↓ ↓H␈↓↓3.11)␈↓ α8                          ␈↓αif␈↓↓ ␈↓αn|␈↓↓chain ␈↓αthen␈↓↓ top ← [ce ← ␈↓αd|␈↓↓ce]
␈↓ ↓H␈↓↓␈↓ α8                          ␈↓αelse␈↓↓ [ce ← ␈↓αd|␈↓↓ce ; 
␈↓ ↓H␈↓↓␈↓ α8                                 ␈↓αa|␈↓↓pos[␈↓αda|␈↓↓chain, ␈↓αaa|␈↓↓chain] ← ce]
␈↓ ↓H␈↓↓␈↓ α8                     ␈↓αelse␈↓↓ ␈↓αif␈↓↓ n > length ce ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ errmsg2[]
␈↓ ↓H␈↓↓␈↓ α8                   pos ← pos[ce, sub1 n]
␈↓ ↓H␈↓↓␈↓ α8                   ␈↓αd|␈↓↓pos ← ␈↓αdd|␈↓↓pos ]

␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓1.␈α
 Write␈α∞programs␈α
to␈α
execute␈α∞the␈α
atomic␈α∞commands␈α
␈↓¬LO,␈α
␈↓and␈α∞␈↓¬RI␈α
␈↓(see␈α
the␈α∞description␈α
of␈α∞␈↓¬RO␈α
␈↓and
␈↓ ↓H␈↓␈↓¬LI␈↓).

␈↓ ↓H␈↓2.␈α
 Modify␈αthe␈α
list␈αcommands␈α
to␈αtake␈α
a␈αnegative␈α
argment␈αwhich␈α
is␈αinterpreted␈α
as␈αcount␈α
from␈αthe
␈↓ ↓H␈↓end␈α
rather␈α
that␈α
the␈α
beginning␈α
of␈α
the␈α␈↓↓ce.␈α
 3.␈↓␈α
 Write␈α
a␈α
programs␈α
for␈α
executing␈α
list-commands␈αof␈α
the
␈↓ ↓H␈↓form␈α
(␈↓¬R␈α∞␈↓<e1>␈α
<e2>)␈α∞(which␈α
means␈α
to␈α∞replace␈α
occurrences␈α∞of␈α
<e1>␈α
as␈α∞elements␈α
of␈α∞the␈α
␈↓↓ce␈↓␈α∞by␈α
<e2>)
␈↓ ↓H␈↓and␈α(␈↓¬TR␈α
␈↓<e1>␈α<e2>)␈α
(which␈αmeans␈α
to␈αreplace␈αoccurrences␈α
of␈α<e1>␈α
as␈αsubexpression␈α
(at␈αany␈αlevel)␈α
of
␈↓ ↓H␈↓␈↓↓ce␈↓␈αby␈α<e2>).␈α Note␈αthat␈αit␈αis␈αadvisable␈αto␈αmake␈αa␈αnew␈αcopy␈αof␈α<e2>␈αfor␈αeach␈αreplacement␈α
otherwise
␈↓ ↓H␈↓in␈α
later␈α
editing␈α∞a␈α
change␈α
to␈α
one␈α∞occurrence␈α
may␈α
have␈α
the␈α∞undesired␈α
side␈α
effects␈α
of␈α∞changing␈α
all
␈↓ ↓H␈↓occurrences.

␈↓ ↓H␈↓4.␈α∞Write␈α∞a␈α
program␈α∞to␈α∞execute␈α
the␈α∞command␈α∞(␈↓¬MV␈α
␈↓<m>␈α∞<n>)␈α∞which␈α
moves␈α∞the␈α∞element␈α∞in␈α
<m>th
␈↓ ↓H␈↓position to the <n>th position.

␈↓ ↓H␈↓5.␈α Consider␈αimplementing␈αan␈α␈↓¬UNDO␈α␈↓feature␈αin␈αthe␈αeditor␈αwhich␈αwould␈αundo␈αthe␈αlast␈αcommand␈α(or
␈↓ ↓H␈↓last␈α⊂n␈α⊃commands).␈α⊂ One␈α⊃way␈α⊂to␈α⊃do␈α⊂this␈α⊂is␈α⊃to␈α⊂notice␈α⊃that␈α⊂commands␈α⊃that␈α⊂make␈α⊃changes␈α⊂have
␈↓ ↓H␈↓inverses.␈α∞ For␈α∞example␈α∞of␈α∞␈↓¬LI␈α∞␈↓is␈α
␈↓¬LO,␈α∞␈↓of␈α∞␈↓¬(I␈α∞<n>␈α∞<exp>)␈↓␈α∞is␈α∞␈↓¬(D␈α
<n>)␈↓,␈α∞etc.␈α∞ All␈α∞we␈α∞need␈α∞is␈α∞for␈α
the
␈↓ ↓H␈↓editor␈αto␈αsetup␈αa␈αlist␈αfor␈αstoring␈αundoing␈α
commands␈αand␈αrequire␈αthat␈αeach␈αcommand␈αthat␈αmakes␈α
a
␈↓ ↓H␈↓change stack its inverse on the undo list.
␈↓ ↓H␈↓142␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓4.  ␈↓αError Handling.␈↓


␈↓ ↓H␈↓        A␈α
robust␈αand␈α
helpful␈α
LISP␈αsystem␈α
will␈α
do␈αall␈α
it␈α
can␈αto␈α
keep␈α
you␈αfrom␈α
destroying␈α
it.␈α This
␈↓ ↓H␈↓includes␈α
noticing␈α
when␈α
you␈α
have␈α∞asked␈α
it␈α
to␈α
evalute␈α
an␈α∞ill␈α
formed␈α
or␈α
illegal␈α
expression.␈α∞ In␈α
this
␈↓ ↓H␈↓class␈αwe␈αinclude␈αattemping␈αto␈αevaluate␈αa␈αvariable␈αthat␈αhasn't␈αbeen␈αbound␈α(␈↓¬SETQ␈↓ed␈αor␈αbound␈αas␈αa
␈↓ ↓H␈↓␈↓¬LAMBDA␈α␈↓or␈α␈↓¬PROG␈α␈↓variable),␈αtaking␈αthe␈α␈↓↓car␈↓␈αor␈α␈↓↓cdr␈↓␈αof␈αan␈αatom␈αor␈αother␈αnon␈αlist␈αstructure␈αobject␈αsuch
␈↓ ↓H␈↓as␈αan␈αarray␈αpointer,␈αattempting␈αto␈αappend␈αa␈αnon-list␈αto␈αsome␈αS-expression,␈αapplying␈αa␈αfunction␈αto
␈↓ ↓H␈↓the␈α
wrong␈α
number␈α
of␈α
arguments,␈α
etc..␈α
 When␈αsuch␈α
an␈α
error␈α
is␈α
noticed␈α
LISP␈α
will␈αcomplain,␈α
printing
␈↓ ↓H␈↓a␈α∞(hopefully␈α∞informative)␈α∞message,␈α
and␈α∞take␈α∞some␈α∞appropriate␈α
action.␈α∞  Other␈α∞errors␈α∞such␈α∞as␈α
an
␈↓ ↓H␈↓infinite␈αrecursion␈α
can't␈αbe␈α
prevented␈αby␈α
inspection␈αof␈α
the␈αexpression␈α
to␈αbe␈α
evaluated,␈αbut␈α
will␈αbe
␈↓ ↓H␈↓noticed␈α∂when␈α⊂some␈α∂abnormal␈α∂state␈α⊂is␈α∂reached␈α∂such␈α⊂as␈α∂when␈α∂the␈α⊂control␈α∂stack␈α∂overflows␈α⊂or␈α∂an
␈↓ ↓H␈↓attempt␈αis␈α
made␈αto␈α
read␈αor␈αwrite␈α
in␈αan␈α
illegal␈αlocation.␈α Again␈α
LISP␈αwill␈α
complain␈αby␈α
printing␈αa
␈↓ ↓H␈↓message␈α∂saying␈α∂what␈α∂abnormal␈α∂condition␈α∂was␈α∂noticed␈α∂and␈α∂take␈α∂appropriate␈α∂action.␈α∂  Of␈α∞course
␈↓ ↓H␈↓there␈αis␈α
always␈αthe␈α
possibility␈αof␈αa␈α
truly␈αdisastrous␈α
error␈αfrom␈αwhich␈α
there␈αis␈α
no␈αrecovery␈αand␈α
here
␈↓ ↓H␈↓there␈α
is␈αnot␈α
much␈αto␈α
do␈αbut␈α
restart␈αand␈α
proceed␈α
with␈αcaution.␈α
 The␈αquestion␈α
remains␈αas␈α
to␈αwhat␈α
to
␈↓ ↓H␈↓do␈α
when␈α
an␈α
error␈α
is␈α∞noticed.␈α
 Typically␈α
LISP␈α
will␈α
enter␈α∞a␈α
state␈α
where␈α
a␈α
debugging␈α∞package␈α
can
␈↓ ↓H␈↓take␈α∩over␈α⊃and␈α∩help␈α∩you␈α⊃find␈α∩out␈α⊃what␈α∩is␈α∩amiss.␈α⊃ In␈α∩this␈α⊃state␈α∩you␈α∩will␈α⊃be␈α∩able␈α∩to␈α⊃evaluate
␈↓ ↓H␈↓expressions,␈α∩possibly␈α∩back␈α∩up␈α∩the␈α∩computation␈α∩some␈α∩number␈α∩of␈α∩steps,␈α∩have␈α∩the␈α⊃computation
␈↓ ↓H␈↓proceed␈α
(after␈α
providing␈α
whatever␈α
information␈α
was␈αlacking␈α
and␈α
caused␈α
the␈α
error)␈α
or␈αabandon␈α
ship
␈↓ ↓H␈↓and return to the top level to start again.

␈↓ ↓H␈↓        In␈α
order␈α
to␈α
aid␈α
in␈α
error␈α
recovery␈α
and␈α
generally␈α
provide␈α
the␈α
user␈α
with␈α
the␈α
ability␈α
to␈αaccess
␈↓ ↓H␈↓information␈α
about␈α
the␈α
state␈α
of␈α
a␈α
computation,␈α
LISP␈α
provides␈α
various␈α
means␈α
of␈α
altering␈αthe␈α
normal
␈↓ ↓H␈↓progress␈α
of␈α∞evaluation␈α
and␈α∞examining␈α
and␈α∞modifying␈α
the␈α∞environment.␈α
 One␈α∞such␈α
means␈α∞is␈α
the
␈↓ ↓H␈↓break␈αloop.␈α Evaluating␈α
␈↓↓break[name,test]␈↓␈αcauses␈αLISP␈α
to␈αevaluate␈α␈↓↓test␈↓␈αand␈α
if␈αit␈αis␈α
not␈α␈↓¬NIL␈↓␈αto␈αgo␈α
in
␈↓ ↓H␈↓to␈αa␈αbreak␈αloop.␈α
 The␈α␈↓↓name␈↓␈αargument␈αis␈α
printed␈αout␈αas␈αa␈α
message␈αand␈αthen␈αa␈α
pseudo␈αread-eval-
␈↓ ↓H␈↓print␈αloop␈α
is␈αentered,␈αwhere␈α
the␈αexpression␈αread␈α
is␈αtested␈αto␈α
see␈αif␈αit␈α
is␈αone␈αof␈α
a␈αfew␈αspecial␈α
"escape
␈↓ ↓H␈↓from␈α∂the␈α∂loop"␈α∂commands.␈α∂ If␈α∞so␈α∂the␈α∂loop␈α∂is␈α∂exited␈α∞in␈α∂the␈α∂manner␈α∂indicated␈α∂by␈α∂the␈α∞command.
␈↓ ↓H␈↓Otherwise␈αthe␈αform␈αis␈αevaled,␈αprinted␈αand␈αwe␈αare␈αback␈αat␈αthe␈αtop␈αof␈αthe␈αloop.␈α There␈αare␈αat␈αleast
␈↓ ↓H␈↓two␈αmodes␈αof␈αreturn␈αfrom␈αa␈αbreak␈αloop.␈α One␈αis␈αjust␈αto␈αreturn␈αto␈αthe␈αtop␈αlevel,␈αaborting␈αwhatever
␈↓ ↓H␈↓computation␈α
the␈αbreak␈α
occured␈α
in,␈α and␈α
the␈α
other␈αis␈α
to␈α
resume␈αthe␈α
computation␈α
(possibly␈αhaving
␈↓ ↓H␈↓altered the state of the world before doing so).

␈↓ ↓H␈↓        To␈αget␈αa␈αcomplete␈αpicture␈αof␈αthe␈αstate␈αof␈αthe␈αcomputation␈αit␈αis␈αuseful␈αto␈αknow␈αthe␈αsequence
␈↓ ↓H␈↓of␈αevents␈αthat␈αled␈αup␈αto␈αthe␈αcurrent␈αstate.␈α This␈αis␈αmade␈αpossible␈αby␈αstacking␈αsuch␈αinformation␈αas
␈↓ ↓H␈↓evaluation␈α∪proceeds.␈α∪ Just␈α∪how␈α∪this␈α∪is␈α∪done␈α∪and␈α∪what␈α∪information␈α∪is␈α∪kept␈α∪depends␈α∀on␈α∪the
␈↓ ↓H␈↓particular␈α∃implementation.␈α∃ For␈α∃the␈α∃sake␈α∃of␈α∃example␈α∃we␈α∃will␈α∃base␈α∃our␈α∃description␈α∃on␈α∃the
␈↓ ↓H␈↓MACLISP␈αimplementation.␈α
 Imagine␈αthat␈α
there␈αis␈αa␈α
stack␈α(called␈α
the␈αspecpdl)␈α
where␈αeach␈αtime␈α
the
␈↓ ↓H␈↓interpreter␈αbegins␈αto␈αevaluate␈αan␈αS-expression␈αit␈αpushes␈αonto␈αthe␈αstack␈αthe␈αfollowing␈αinformation:
␈↓ ↓H␈↓the␈α
current␈α
specpdl␈α
pointer,␈α
the␈α
expression␈α
to␈α
be␈α
evaluated,␈α
an␈α
a␈α
pointer␈α
to␈α
the␈α
current␈α
variable
␈↓ ↓H␈↓binding␈α∂environment,␈α⊂(MACLISPs␈α∂version␈α∂of␈α⊂the␈α∂a-list␈α∂argument␈α⊂to␈α∂␈↓↓eval).␈↓␈α∂ The␈α⊂current␈α∂stack
␈↓ ↓H␈↓pointer␈α⊂is␈α∂then␈α⊂reset␈α⊂to␈α∂the␈α⊂new␈α∂stack␈α⊂top␈α⊂and␈α∂evaluation␈α⊂continues.␈α∂ When␈α⊂evaluation␈α⊂of␈α∂the
␈↓ ↓H␈↓expression␈α∂is␈α∞complete␈α∂that␈α∂collection␈α∞of␈α∂information␈α∂is␈α∞popped␈α∂from␈α∂the␈α∞stack.␈α∂ There␈α∂are␈α∞two
␈↓ ↓H␈↓primitive␈α∀operations␈α∃associated␈α∀with␈α∀this␈α∃stack.␈α∀ One,␈α∀␈↓↓evalframe[pdl],␈↓␈α∃returns␈α∀a␈α∀list␈α∃of␈α∀the
␈↓ ↓H␈↓information␈αat␈αthe␈αstack␈αposition␈αindicated␈αby␈αpdl.␈α Thus␈αwe␈αcan␈αuse␈αthis␈αinformation␈αto␈αmove␈αup
␈↓ ↓H␈↓and␈α⊃down␈α⊃the␈α⊃stack␈α⊂and␈α⊃evaluate␈α⊃expressions␈α⊃in␈α⊂various␈α⊃environments.␈α⊃ The␈α⊃other␈α⊂operation
␈↓ ↓H␈↓causes␈α⊂the␈α⊂interpreter␈α⊂to␈α⊂change␈α⊂its␈α⊂notion␈α⊂about␈α⊂the␈α⊂current␈α⊂top␈α⊂of␈α⊂the␈α⊂stack.␈α⊂ This␈α⊂is␈α∂called
␈↓ ↓H␈↓␈↓↓freturn␈↓␈α∩(for␈α⊃forced␈α∩return).␈α⊃ If␈α∩␈↓↓freturn[pdl,exp]␈↓␈α⊃is␈α∩evaluated␈α⊃then␈α∩the␈α⊃interpreter␈α∩behaves␈α⊃as
␈↓ ↓H␈↓though␈α∂␈↓↓pdl␈↓␈α⊂were␈α∂the␈α∂top␈α⊂of␈α∂the␈α∂stack␈α⊂and␈α∂the␈α∂value␈α⊂of␈α∂␈↓↓exp␈↓␈α∂were␈α⊂the␈α∂result␈α∂of␈α⊂evaluating␈α∂the
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠143


␈↓ ↓H␈↓expression␈α
stored␈αthere.␈α
 It␈α
thus␈α"returns"␈α
from␈α
that␈αstate␈α
and␈α
proceeds.␈α This␈α
is␈α
a␈αhighly␈α
non-local
␈↓ ↓H␈↓form␈α∂of␈α∂␈↓αgo to␈↓␈α∂and␈α∞should␈α∂be␈α∂used␈α∂with␈α∞caution,␈α∂however␈α∂it␈α∂is␈α∞most␈α∂useful␈α∂for␈α∂building␈α∞error-
␈↓ ↓H␈↓recovery and debugging routines.

␈↓ ↓H␈↓        Finally␈α⊃there␈α⊂are␈α⊃primitives␈α⊂for␈α⊃inducing␈α⊃and␈α⊂trapping␈α⊃errors.␈α⊂ The␈α⊃main␈α⊃primitives␈α⊂of
␈↓ ↓H␈↓interest␈αare␈αthe␈αpair␈αof␈αfunctions␈α␈↓↓error␈↓␈αand␈α␈↓↓errset.␈↓␈α  In␈αthe␈αsimplest␈αcase␈α␈↓↓error␈↓␈αtakes␈αone␈αargument
␈↓ ↓H␈↓which␈αis␈α
a␈αmessage␈α
to␈αbe␈α
printed.␈α Evaluation␈α of␈α
␈↓↓error␈αmsg␈↓␈α
 causes␈αthe␈α
value␈αof␈α
␈↓↓msg␈↓␈αto␈αbe␈α
printed
␈↓ ↓H␈↓and␈αa␈α
LISP␈αerror␈α
to␈αbe␈α
signaled.␈α The␈α
result␈αof␈αsignalling␈α
an␈αerror␈α
is␈αpopping␈α
up␈αto␈α
the␈αnearest
␈↓ ↓H␈↓␈↓↓errset␈↓␈α
or␈α
to␈α
the␈α
top␈α
level␈α
of␈α
LISP␈α
if␈α
there␈α
is␈α
no␈α
enclosing␈α
␈↓↓errset.␈↓␈α
 The␈α
value␈α
returned␈α
is␈α
␈↓¬NIL␈↓.␈α
 ␈↓↓errset␈↓
␈↓ ↓H␈↓has␈αtwo␈αarguments,␈α
the␈αfirst␈αis␈α
an␈αexpression␈αto␈α
be␈αevaluated␈αand␈α
the␈αsecond␈αis␈α
a␈αflag.␈α If␈αan␈α
error
␈↓ ↓H␈↓occurs␈αwhile␈αevaluating␈αthe␈αexpression,␈αthe␈α␈↓↓errset␈↓␈α"traps"␈αthe␈αerror␈αand␈αreturns␈αthe␈αvalue␈αreturned
␈↓ ↓H␈↓by␈α
the␈α
error␈αfunction,␈α
otherwise␈α
␈↓↓errset␈↓␈αreturns␈α
a␈α
list␈α
of␈αone␈α
element,␈α
the␈αvalue␈α
of␈α
the␈αexpression.␈α
 If
␈↓ ↓H␈↓the␈α
flag␈α
is␈α
off␈α(eg.␈α
if␈α
it␈α
is␈α
␈↓¬NIL␈↓)␈αthen␈α
no␈α
error␈α
messages␈α
are␈αprinted.␈α
 The␈α
reason␈α
for␈α␈↓↓errset␈↓␈α
returning
␈↓ ↓H␈↓a␈αlist␈αcontaining␈αthe␈αvalue␈αof␈αthe␈αsuccessfully␈αevaluated␈αexpression␈αis␈αto␈αdistinguish␈αthe␈αvalue␈α␈↓¬NIL␈↓
␈↓ ↓H␈↓from␈αthe␈αatom␈α␈↓¬NIL␈↓␈αwhich␈αsays␈αthat␈αan␈αerror␈αhas␈αoccurred.␈α An␈αalternate␈αform␈αof␈αerror␈αgeneration
␈↓ ↓H␈↓is␈α␈↓↓err␈↓␈αwhich␈αtakes␈αa␈αsingle␈αargument.␈α Evaluation␈αof␈α␈↓↓err␈αx␈↓␈αcauses␈αan␈αerror␈αto␈αbe␈αsignaled␈αand␈αthe
␈↓ ↓H␈↓value␈α
of␈α
␈↓↓x␈↓␈α∞is␈α
returned.␈α
 There␈α
are␈α∞many␈α
variations␈α
and␈α
extensions␈α∞of␈α
the␈α
above␈α∞error␈α
functions
␈↓ ↓H␈↓which you should learn about for your particular implementation of LISP.

␈↓ ↓H␈↓        In␈α∞addition␈α
to␈α∞trapping␈α∞errors,␈α
the␈α∞␈↓↓err,␈↓␈α
␈↓↓errset␈↓␈α∞mechanism␈α∞can␈α
be␈α∞used␈α
to␈α∞return␈α∞from␈α
the
␈↓ ↓H␈↓midst␈α∞of␈α
a␈α∞computation␈α
when␈α∞the␈α
answer␈α∞has␈α
been␈α∞found␈α
and␈α∞it␈α
is␈α∞not␈α
necessary␈α∞to␈α∞process␈α
the
␈↓ ↓H␈↓result␈αfurther.␈α
  For␈αexample␈α
if␈αyou␈α
are␈αlooking␈αfor␈α
a␈αparticular␈α
atom␈αin␈α
an␈αS-expression␈αby␈α
doing
␈↓ ↓H␈↓a␈αrecursive␈αsearch,␈αthen␈αif␈αthe␈αatom␈αis␈αfound,␈αexecuting␈α␈↓↓err␈↓␈αpops␈αdirectly␈αback␈αto␈αthe␈αcalling␈α␈↓↓errset␈↓
␈↓ ↓H␈↓without␈αdoing␈α
the␈αintermediate␈αbook␈α
keeping␈αnecessary␈α
to␈αreturn␈αthrough␈α
many␈αlevels␈αof␈α
recursive
␈↓ ↓H␈↓calls.␈α⊃ MACLISP␈α⊃provides␈α⊃a␈α⊃"structured"␈α⊃form␈α⊃of␈α⊃such␈α⊃non-local␈α⊃returns␈α⊃in␈α⊃the␈α⊃form␈α⊃of␈α⊃the
␈↓ ↓H␈↓␈↓↓catch/throw␈↓␈α
construct.␈α
 Each␈α
takes␈α
two␈α
arguments␈α
an␈α
expression␈α
to␈α
evaluate␈α
and␈α
a␈α
tag.␈α
 ␈↓↓catch[e,tag]␈↓
␈↓ ↓H␈↓is␈α∂evaluated␈α∂by␈α∞evaluating␈α∂␈↓↓e.␈↓␈α∂  If␈α∞an␈α∂expression␈α∂of␈α∞the␈α∂form␈α∂␈↓↓throw[x,tag1]␈↓␈α∞is␈α∂evaluated␈α∂in␈α∞the
␈↓ ↓H␈↓course␈αof␈αevauating␈α␈↓↓e␈↓␈α
and␈α␈↓↓tag=tag1␈↓␈αthen␈αthe␈α
evaluation␈αof␈α␈↓↓e␈↓␈αis␈αabandonded␈α
an␈αthe␈αvalue␈αof␈α
␈↓↓x␈↓␈αis
␈↓ ↓H␈↓returned␈αas␈αthe␈αvalue␈αof␈αthe␈α␈↓↓catch.␈↓␈α If␈αthe␈αevaluation␈αof␈α␈↓↓e␈↓␈αis␈αcompleted␈αthen␈αits␈αvalue␈αis␈αreturned
␈↓ ↓H␈↓as␈α⊃the␈α∩value␈α⊃of␈α⊃the␈α∩␈↓↓catch.␈↓␈α⊃ We␈α⊃will␈α∩have␈α⊃more␈α⊃to␈α∩say␈α⊃about␈α⊃this␈α∩and␈α⊃other␈α∩mechanisms␈α⊃of
␈↓ ↓H␈↓controlling evaluation of expressions in a later chapter.



␈↓ ↓H␈↓5.  ␈↓αDebugging Aids in LISP.␈↓


␈↓ ↓H␈↓        In␈α
some␈α
cases␈αlooking␈α
at␈α
the␈αlist␈α
of␈α
function␈αcalls␈α
leading␈α
up␈αto␈α
an␈α
error␈αand␈α
at␈α
the␈αvalues␈α
of
␈↓ ↓H␈↓some␈α
of␈α
the␈αvariables␈α
is␈α
sufficient␈α
to␈αpin␈α
point␈α
an␈α
error␈αin␈α
a␈α
program,␈α
however␈αit␈α
is␈α
often␈αuseful␈α
to
␈↓ ↓H␈↓have␈αmore␈αpowerful␈αdebugging␈αfacilities␈αavailable.␈α We␈αwill␈αlook␈αat␈αtwo␈αkinds␈αof␈αdebugging␈αaids.
␈↓ ↓H␈↓The␈αfirst␈αis␈αthe␈αsort␈αof␈αdebugging␈αpackage␈αthat␈αmodifies␈αfunction␈αdefinitions␈αto␈αprint␈αinformation
␈↓ ↓H␈↓about␈α∩the␈α∩state␈α⊃of␈α∩the␈α∩computation␈α∩and␈α⊃perhaps␈α∩interact␈α∩with␈α⊃the␈α∩user.␈α∩ This␈α∩includes␈α⊃such
␈↓ ↓H␈↓features␈α
as␈α
tracing␈α
and␈α
breaking.␈α
 The␈α
second␈α
is␈α
the␈α
sort␈α
of␈α
program␈α
that␈α
will␈α
take␈α
over␈α
after␈α
a
␈↓ ↓H␈↓break loop has been entered and help you figure out what has happened.

␈↓ ↓H␈↓        When␈α⊂a␈α⊂function␈α⊂is␈α⊂traced,␈α⊂each␈α⊂time␈α⊂it␈α⊂is␈α⊂called␈α⊂information␈α⊂is␈α⊂printed␈α⊂out␈α⊃giving␈α⊂the
␈↓ ↓H␈↓function␈α∞name␈α
and␈α∞the␈α
values␈α∞of␈α
the␈α∞arguments.␈α
 When␈α∞it␈α
returns␈α∞a␈α
value␈α∞an␈α
additional␈α∞line␈α
is
␈↓ ↓H␈↓printed␈αout␈αgiving␈αthe␈αname␈αof␈αthe␈αfunction␈αand␈αthe␈αvalue␈αbeing␈αreturned.␈α In␈αorder␈αto␈αmake␈αthe
␈↓ ↓H␈↓output␈α∞somewhat␈α∞more␈α∞readable,␈α∂the␈α∞printed␈α∞lines␈α∞could␈α∞be␈α∂indented␈α∞according␈α∞to␈α∞the␈α∂depth␈α∞of
␈↓ ↓H␈↓nesting␈α
of␈α
calls␈α
to␈α
a␈αtraced␈α
function.␈α
  To␈α
trace␈α
a␈αfunction␈α
one␈α
replaces␈α
the␈α
the␈α
original␈αfunction
␈↓ ↓H␈↓144␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓definition␈α⊂with␈α∂a␈α⊂program␈α∂that␈α⊂does␈α⊂the␈α∂initial␈α⊂printing,␈α∂applies␈α⊂the␈α∂original␈α⊂definition␈α⊂to␈α∂the
␈↓ ↓H␈↓arguments,␈α∞does␈α∞the␈α∞final␈α
printing␈α∞and␈α∞returns␈α∞the␈α∞value.␈α
 This␈α∞program␈α∞is␈α∞also␈α∞responsible␈α
for
␈↓ ↓H␈↓updating any global variables that are used to keep track of the recursion depth of a call.

␈↓ ↓H␈↓        Thus we could build a small trace package using the following programs.

␈↓ ↓H␈↓␈↓ β8␈↓↓trace fn ← ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αprogram␈↓↓ [def]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αif␈↓↓ get[fn, ␈↓¬TRACED␈↓↓] ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ ␈↓¬ALREADY-TRACED␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓def ← get[fn, ␈↓¬EXPR␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αif␈↓↓ ␈↓αn|␈↓↓def ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ ␈↓¬NOT-DEFINED␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αif␈↓↓ ¬[␈↓αa|␈↓↓def = ␈↓¬LAMBDA␈↓↓] ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ ␈↓¬NOT-LAMBDA␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓putprop[fn, ␈↓¬T␈↓↓, ␈↓¬TRACED␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓putprop[fn, def, ␈↓¬!OLDDEF␈↓↓]␈↓
␈↓ ↓H␈↓5.1)␈↓ ∧8␈↓↓putprop[fn, ␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓subst[fn, ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓↓␈↓¬?FN␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ ¬X␈↓↓subst[␈↓αad|␈↓↓def, ␈↓
␈↓ ↓H␈↓␈↓ ε_␈↓↓␈↓¬?ARGS␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ ε_␈↓↓subst[␈↓¬LIST␈↓↓ . ␈↓αad|␈↓↓def, ␈↓
␈↓ ↓H␈↓␈↓ εX␈↓↓␈↓¬*ARGS␈↓↓, ␈↓
␈↓ ↓H␈↓␈↓ εX␈↓↓get[␈↓¬!TRACE␈↓↓, ␈↓¬!PATTERN␈↓↓]]]], ␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓↓␈↓¬EXPR␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αreturn␈↓↓ ␈↓¬OK␈↓↓␈↓

␈↓ ↓H␈↓␈↓ β8␈↓↓untrace fn ← ␈↓
␈↓ ↓H␈↓␈↓ βx␈↓↓␈↓αprogram␈↓↓ [def]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αif␈↓↓ ¬get[fn, ␈↓¬TRACED␈↓↓] ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ ␈↓¬NOT-TRACED␈↓↓␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓def ← get[fn, ␈↓¬!OLDDEF␈↓↓]␈↓
␈↓ ↓H␈↓5.2)␈↓ ∧8␈↓↓putprop[fn, ␈↓¬NIL␈↓↓, ␈↓¬TRACED␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓remprop[fn, ␈↓¬!OLDDEF␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓putprop[fn, def, ␈↓¬EXPR␈↓↓]␈↓
␈↓ ↓H␈↓␈↓ ∧8␈↓↓␈↓αreturn␈↓↓ ␈↓¬OK␈↓↓␈↓


␈↓ ↓H␈↓In order for the ␈↓↓trace␈↓ routine to work we define the ␈↓¬!PATTERN␈↓ property by evaluating

␈↓ ↓H␈↓¬␈↓↓defprop[␈↓¬!TRACE,
␈↓ ↓H␈↓¬        (LAMBDA ?ARGS
␈↓ ↓H␈↓¬          (PROG (!VAL)
␈↓ ↓H␈↓¬            (TERPRI) (MARKS !ILEVEL)
␈↓ ↓H␈↓¬            (PRINC (QUOTE Entering )) (PRINC (QUOTE ?FN))
␈↓ ↓H␈↓¬            (PROG (ARGL)
␈↓ ↓H␈↓¬              (SETQ ARGL (QUOTE ?ARGS))
␈↓ ↓H␈↓¬              L1
␈↓ ↓H␈↓¬              (COND ((NULL ARGL) (RETURN NIL)))
␈↓ ↓H␈↓¬              (TERPRI) (INDENT (PLUS !ILEVEL 2))
␈↓ ↓H␈↓¬              (PRINC (CAR ARGL)) (PRINC (QUOTE  = )) (PRINC (EVAL (CAR ARGL)))
␈↓ ↓H␈↓¬              (SETQ ARGL (CDR ARGL))
␈↓ ↓H␈↓¬              (GO L1))
␈↓ ↓H␈↓¬            (SETQ !VAL
␈↓ ↓H␈↓¬                  ((LAMBDA (!ILEVEL)
␈↓ ↓H␈↓¬                     (APPLY (GET (QUOTE ?FN) (QUOTE !OLDDEF)) *ARGS))
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠145


␈↓ ↓H␈↓¬                   (ADD1 !ILEVEL)))
␈↓ ↓H␈↓¬            (TERPRI) (INDENT !ILEVEL)
␈↓ ↓H␈↓¬            (PRINC (QUOTE Returning from )) (PRINC (QUOTE ?FN))
␈↓ ↓H␈↓¬            (PRINC (QUOTE  with )) (PRINC !VAL)
␈↓ ↓H␈↓¬            (TERPRI)
␈↓ ↓H␈↓¬            (RETURN !VAL))),
␈↓ ↓H␈↓¬        !PATTERN␈↓↓]␈↓¬

␈↓ ↓H␈↓The␈αprograms␈α␈↓↓indent␈↓␈αand␈α␈↓↓marks␈↓␈αare␈αused␈αby␈αa␈αtraced␈αprogram␈αto␈αprint␈α␈↓↓n␈↓␈αblanks␈αor␈α␈↓↓n␈↓␈αmarks␈αsay
␈↓ ↓H␈↓">".  We give the definition of ␈↓↓indent.␈↓  The definition of ␈↓↓marks␈↓ is similiar.

␈↓ ↓H␈↓↓␈↓ β8indent n ←
␈↓ ↓H␈↓↓␈↓ βx␈↓αprogram␈↓↓[i]
␈↓ ↓H␈↓↓␈↓ ∧8i←1
␈↓ ↓H␈↓↓␈↓ βxloop
␈↓ ↓H␈↓↓5.3)␈↓ ∧8␈↓αif␈↓↓ i>n ␈↓αthen␈↓↓ ␈↓αreturn␈↓↓ ␈↓¬NIL␈↓↓
␈↓ ↓H␈↓↓␈↓ ∧8princ " "
␈↓ ↓H␈↓↓␈↓ ∧8i←i+1
␈↓ ↓H␈↓↓␈↓ ∧8␈↓αgo to␈↓↓ loop

␈↓ ↓H␈↓Before evaluating a traced function it is necessary to initialize the global variable ␈↓¬!ILEVEL␈↓.

␈↓ ↓H␈↓For example if we say to LISP

␈↓ ↓H␈↓¬␈↓ αh(!TRACE READ)
␈↓ ↓H␈↓¬␈↓ αh(!TRACE READA)
␈↓ ↓H␈↓¬␈↓ αh(SETQ !ILEVEL 0)
␈↓ ↓H␈↓¬␈↓ αh(READ '(LP LP A RP A DOT B RP))

␈↓ ↓H␈↓the following will be printed


␈↓ ↓H␈↓¬␈↓ αhEntering READ
␈↓ ↓H␈↓¬␈↓ αh  U = (LP LP A RP A DOT B RP)
␈↓ ↓H␈↓¬␈↓ αh>Entering READA
␈↓ ↓H␈↓¬␈↓ αh   U = (LP A RP A DOT B RP)
␈↓ ↓H␈↓¬␈↓ αh   L = NIL
␈↓ ↓H␈↓¬␈↓ αh>>Entering READA
␈↓ ↓H␈↓¬␈↓ αh    U = (A RP A DOT B RP)
␈↓ ↓H␈↓¬␈↓ αh    L = NIL
␈↓ ↓H␈↓¬␈↓ αh>>>Entering READA
␈↓ ↓H␈↓¬␈↓ αh     U = (RP A DOT B RP)
␈↓ ↓H␈↓¬␈↓ αh     L = (A)
␈↓ ↓H␈↓¬␈↓ αh   Returning from READA with ((A) A DOT B RP)
␈↓ ↓H␈↓¬␈↓ αh  Returning from READA with ((A) A DOT B RP)
␈↓ ↓H␈↓¬␈↓ αh>>Entering READA
␈↓ ↓H␈↓¬␈↓ αh    U = (A DOT B RP)
␈↓ ↓H␈↓¬␈↓ αh    L = ((A))
␈↓ ↓H␈↓¬␈↓ αh>>>Entering READA
␈↓ ↓H␈↓¬␈↓ αh     U = (DOT B RP)
␈↓ ↓H␈↓¬␈↓ αh     L = (A (A))
␈↓ ↓H␈↓¬␈↓ αh>>>>Entering READA
␈↓ ↓H␈↓¬␈↓ αh      U = (B RP)
␈↓ ↓H␈↓¬␈↓ αh      L = NIL
␈↓ ↓H␈↓¬␈↓ αh>>>>>Entering READA
␈↓ ↓H␈↓¬␈↓ αh       U = (RP)
␈↓ ↓H␈↓146␈↓ ¬wChapter  VI␈↓ H


␈↓ ↓H␈↓¬␈↓ αh       L = (B)
␈↓ ↓H␈↓¬␈↓ αh     Returning from READA with ((B))
␈↓ ↓H␈↓¬␈↓ αh    Returning from READA with ((B))
␈↓ ↓H␈↓¬␈↓ αh   Returning from READA with (((A) A . B))
␈↓ ↓H␈↓¬␈↓ αh  Returning from READA with (((A) A . B))
␈↓ ↓H␈↓¬␈↓ αh Returning from READA with (((A) A . B))
␈↓ ↓H␈↓¬␈↓ αhReturning from READ with ((A) A . B)

␈↓ ↓H␈↓¬␈↓ αh((A) A . B)

␈↓ ↓H␈↓        The␈αother␈αcase␈αof␈αdebugging␈αby␈αmodification␈αof␈αfunction␈αdefinitions␈αis␈αbreaking.␈α The␈αidea
␈↓ ↓H␈↓is␈αto␈αmodify␈αa␈αfunction␈αby␈αputting␈α"break␈αpoints"␈αin␈αthe␈αcode.␈α The␈αbreaks␈αmay␈αbe␈αconditional␈αor
␈↓ ↓H␈↓unconditional,␈αthey␈αmay␈αbe␈αinserted␈αat␈αthe␈αbeginning␈αor␈αbefore␈αor␈αafter␈αcertain␈αexpressions␈αin␈αthe
␈↓ ↓H␈↓code.␈α
 Thus␈α
we␈α
might␈αhave␈α
a␈α
program␈α
␈↓↓mkbreaks[fn,clauses]␈↓␈αwhich␈α
saves␈α
the␈α
original␈αdefinition␈α
of
␈↓ ↓H␈↓␈↓↓fn,␈↓␈αsets␈αa␈αflag␈αsaying␈αit␈αis␈αbroken,␈αmakes␈αthe␈αmodifications␈αindicated␈αby␈αthe␈αlist␈αof␈αbreak␈αclauses,
␈↓ ↓H␈↓and␈α
makes␈α
the␈α
new␈α
definition.␈α
 Each␈α
break␈α
clause␈α
describes␈α
a␈α
position␈α
to␈α
place␈α
a␈α
break␈α
and␈αthe
␈↓ ↓H␈↓condition under which the break is to occur (the test expression).

␈↓ ↓H␈↓        Given␈α⊂the␈α⊂ability␈α⊂to␈α⊂"break"␈α⊂the␈α⊂evaluation␈α⊂of␈α⊂an␈α⊂expression␈α⊂we␈α⊂now␈α⊂need␈α⊃some␈α⊂useful
␈↓ ↓H␈↓routines␈αfor␈αexamining␈αthe␈α
state␈αof␈αthe␈αworld.␈α The␈α
functions␈α␈↓↓evalframe␈↓␈αand␈α␈↓↓freturn␈↓␈α(or␈α
analogous
␈↓ ↓H␈↓functions)␈α∞provide␈α∞us␈α∞with␈α∞the␈α∞necessary␈α∞primitives,␈α∞but␈α∞not␈α∞it␈α∞the␈α∞most␈α∞useful␈α∞form.␈α∞ A␈α∞typical
␈↓ ↓H␈↓␈↓↓helper␈↓␈αprogram␈αmight␈αinclude␈αoperations␈αfor␈αmoving␈αup␈αand␈αdown␈αthe␈αstack,␈αprinting␈αthe␈αcurrent
␈↓ ↓H␈↓expression,␈αevaluting␈α
expressions␈αin␈αthe␈α
current␈αenvironment,␈αand␈α
forcing␈αthe␈αbroken␈α
computation
␈↓ ↓H␈↓to␈α⊂continue.␈α⊂ Then␈α⊂in␈α⊂a␈α⊂break␈α⊂loop␈α⊂␈↓↓helper␈↓␈α⊂could␈α⊂be␈α⊂invoked,␈α⊂and␈α⊂would␈α⊂go␈α⊂into␈α⊃a␈α⊂command
␈↓ ↓H␈↓reading␈αloop.␈α Notice␈αthat␈αmoving␈αdown␈αthe␈αstack␈α(backwards␈αin␈αtime)␈αcan␈αbe␈αdone␈αby␈αalternately
␈↓ ↓H␈↓applying␈α
␈↓↓evalframe␈↓␈αand␈α
selecting␈α
the␈αpdl␈α
pointer␈αfrom␈α
the␈α
list.␈α However␈α
moving␈α
back␈αup␈α
is␈αnot␈α
so
␈↓ ↓H␈↓easy.  The ␈↓↓helper␈↓ program will need to maintain its own stack of pdl pointers.

␈↓ ↓H␈↓α␈↓ ε
Exercises

␈↓ ↓H␈↓1.␈α Add␈αsome␈αadditional␈αfeatures␈αto␈αthe␈αtracing␈αprogram.␈α For␈αexample␈αit␈αwould␈αbe␈αuseful␈αto␈α
have
␈↓ ↓H␈↓only␈α⊃selected␈α⊃arguments␈α⊃to␈α⊃a␈α⊃function␈α⊃printed␈α⊃at␈α⊃each␈α⊃call.␈α⊃ Also␈α⊃printing␈α⊃the␈α⊃value␈α⊃of␈α⊂some
␈↓ ↓H␈↓additional␈αexpression␈αbefore␈αor␈αafter,␈α(or␈αboth)␈αthe␈αevaluation␈αthe␈αfunction␈αapplication.␈α You␈αmay
␈↓ ↓H␈↓think of other features you would like to have in a tracing package.

␈↓ ↓H␈↓2.␈α Write␈αa␈αprogram␈αthat␈αadds␈αbreak␈αpoints␈αto␈αfunction␈αdefinitions.␈α You␈αwill␈αneed␈αto␈αdecide␈αhow
␈↓ ↓H␈↓the break points are to be specified.

␈↓ ↓H␈↓3.␈α Write␈α
code␈αto␈α
execute␈αsome␈α␈↓↓helper␈↓␈α
commands.␈α  Use␈α
the␈α␈↓↓evalframe␈α
function␈↓␈αdescribed␈αin␈α
section
␈↓ ↓H␈↓␈↓π∞␈↓4.␈α Assume␈αthat␈α␈↓¬NIL␈↓␈αrepresents␈αthe␈αpointer␈αto␈α
the␈αcurrent␈αtop␈αof␈αthe␈αstack.␈α Use␈αand␈αmaintain␈α
the
␈↓ ↓H␈↓global␈αvariables␈α␈↓¬POINT␈α␈↓and␈α␈↓¬PTLIST␈α␈↓which␈αrepresent␈αthe␈αcurrent␈αposition␈αin␈αthe␈αstack␈αand␈αthe␈αlist
␈↓ ↓H␈↓of␈α
stack␈α
positions␈α
from␈α
here␈α
to␈α
the␈α
top␈α
(to␈α
allow␈α
you␈α
to␈α
go␈α
up␈α
as␈α
well␈α
as␈α
down).␈α∞ Implement␈α
the
␈↓ ↓H␈↓commands␈α⊂␈↓¬UP␈α⊃␈↓<n>␈α⊂and␈α⊂␈↓¬DOWN␈α⊃␈↓<n>␈α⊂for␈α⊃moving␈α⊂up␈α⊂and␈α⊃down␈α⊂the␈α⊂stack␈α⊃<n>␈α⊂levels,␈α⊃␈↓¬NEXT␈α⊂␈↓<fn>
␈↓ ↓H␈↓which␈α⊃moves␈α⊃to␈α⊃the␈α⊂next␈α⊃occurrence␈α⊃of␈α⊃a␈α⊂call␈α⊃to␈α⊃<fn>␈α⊃down␈α⊂the␈α⊃stack,␈α⊃ ␈↓¬CE␈α⊃␈↓which␈α⊃prints␈α⊂the
␈↓ ↓H␈↓expression␈α⊃being␈α⊃evaluated␈α⊃at␈α⊂the␈α⊃current␈α⊃position,␈α⊃␈↓¬VAL␈α⊂␈↓<exp>␈α⊃which␈α⊃evaluates␈α⊃<exp>␈α⊃in␈α⊂the
␈↓ ↓H␈↓binding␈α⊃environment␈α⊃corresponding␈α∩to␈α⊃the␈α⊃current␈α⊃position␈α∩and␈α⊃prints␈α⊃the␈α⊃result,␈α∩and␈α⊃␈↓¬FREES
␈↓ ↓H␈↓¬␈↓which␈αfinds␈αall␈αfree␈αvariables␈αin␈αthe␈αcurrent␈αexpression␈αand␈αprints␈αthe␈αvariable␈αnames␈αand␈αvalues
␈↓ ↓H␈↓in␈αthe␈α
environment␈αcorresponding␈αto␈α
the␈αcurrent␈α
position.␈α ␈↓¬FREES␈α␈↓should␈α
check␈αthat␈α
the␈αvariable
␈↓ ↓H␈↓is␈αactually␈α
bound␈αin␈α
the␈αcurrent␈α
environment␈αand␈α
if␈αindicate␈α
that␈αfact␈α
rather␈αthat␈α
causing␈αan␈α
error
␈↓ ↓H␈↓by trying to evaluate it.
␈↓ ↓H␈↓␈↓ ¬wChapter  VI␈↓ ≠147


␈↓ ↓H␈↓6.  ␈↓αExercises.␈↓


␈↓ ↓H␈↓1.␈α Write␈αa␈αprogram␈αto␈α"pretty␈αprint"␈αfunction␈αdefinitions.␈α The␈αidea␈αis␈αto␈αbe␈αable␈αto␈αprint␈αwith␈αa
␈↓ ↓H␈↓suitable␈αdivision␈αinto␈α lines␈αand␈αwith␈αindentation␈αso␈αthat␈αthe␈αstructure␈αof␈αthe␈αprogram␈αis␈αeasier␈αto
␈↓ ↓H␈↓discover.␈α∞ The␈α∞program␈α∞may␈α
be␈α∞similar␈α∞to␈α∞␈↓↓prinlis.␈↓␈α
 You␈α∞will␈α∞need␈α∞additional␈α∞special␈α
characters,
␈↓ ↓H␈↓say␈α␈↓¬SPACE␈α
␈↓and␈α␈↓¬NEWLINE.␈α
␈↓␈αThe␈α
program␈αshould␈α
print␈αsubexpressions␈α
on␈αa␈α
single␈αline␈α
when␈αever
␈↓ ↓H␈↓possible,␈αalign␈αarguments␈αin␈αa␈αfunction␈αcall,␈αand␈αpairs␈α
of␈αa␈αconditional␈αwhen␈αthey␈αwill␈αnot␈αfit␈αon␈α
a
␈↓ ↓H␈↓line.␈α∞ You␈α∞will␈α∞probably␈α∞wish␈α∞to␈α∞experiment␈α
to␈α∞see␈α∞how␈α∞much␈α∞indentation␈α∞is␈α∞desired␈α∞in␈α
various
␈↓ ↓H␈↓cases.␈α⊂ You␈α⊂will␈α⊂probably␈α⊂ be␈α⊂able␈α⊂to␈α⊂think␈α⊂of␈α⊂other␈α⊂conventions␈α⊂that␈α⊂will␈α⊂make␈α⊂the␈α⊂resulting
␈↓ ↓H␈↓output easier to read.

␈↓ ↓H␈↓2.␈α
 A␈α
standard␈α
aid␈αin␈α
the␈α
debugging␈α
machine␈αlanguage␈α
programs␈α
is␈α
the␈αability␈α
to␈α
single␈α
step␈αto␈α
the
␈↓ ↓H␈↓execution␈αof␈αa␈αprogram.␈α In␈αthis␈αmode␈αthe␈αprocessor␈αhalts␈αafter␈αthe␈αevaluation␈αof␈αeach␈αinstruction,
␈↓ ↓H␈↓allows␈α∞the␈α∞programmer␈α∞to␈α∞examine␈α∞and␈α
modify␈α∞the␈α∞contents␈α∞of␈α∞regiseters,␈α∞memory␈α∞locations,␈α
the
␈↓ ↓H␈↓program␈α∞counter␈α∞etc..␈α∞ We␈α∂can␈α∞imagine␈α∞the␈α∞process␈α∂of␈α∞interpreting␈α∞and␈α∞S-expression␈α∂and␈α∞being
␈↓ ↓H␈↓composed␈αof␈α
indivisible␈αsteps␈α
such␈αand␈αapplying␈α
primitive␈αfunctions␈α
building␈αan␈α
environment␈αto
␈↓ ↓H␈↓evaluate␈α
the␈α∞body␈α
of␈α
a␈α∞lambda␈α
expression,␈α
a␈α∞branching␈α
decision,␈α
etc..␈α∞ Write␈α
a␈α∞program␈α
␈↓↓stepper␈↓
␈↓ ↓H␈↓which␈α"single-steps"␈αthrough␈αthe␈α
evaluation␈αof␈αan␈αexpression.␈α
 A␈αsimple␈αversion␈αwould␈α prints␈α
out
␈↓ ↓H␈↓each␈αexpression␈α
as␈αit␈αbegins␈α
to␈αevaluate␈αit,␈α
and␈αprint␈αout␈α
values␈αas␈αthey␈α
are␈αobtained.␈α
 A␈αfancier
␈↓ ↓H␈↓version␈α
might␈α
go␈α
into␈α
a␈α
read-eval-print␈α
loop␈α
(maybe␈α
a␈α
break␈α
loop)␈α
so␈α
that␈α
the␈α
programmer␈α
can
␈↓ ↓H␈↓examine␈α
the␈α
state␈α
of␈α
the␈α
computation,␈α
modify␈αthings,␈α
and␈α
then␈α
continue␈α
the␈α
single␈α
steping.␈α You
␈↓ ↓H␈↓may␈α
want␈α
options␈αto␈α
bail␈α
out␈αnow,␈α
or␈α
evaluate␈αthe␈α
current␈α
expression␈αin␈α
fast␈α
mode␈α
(returning␈αto
␈↓ ↓H␈↓single stepping if there is any computation pending.
␈↓ ↓H␈↓␈↓ εH␈↓ ?i


␈↓ ↓H␈↓α␈↓ ¬PBIBLIOGRAPHY





␈↓ ↓H␈↓␈↓αAllen, John R. [1979]␈↓:  ␈↓↓The Anatomy of LISP␈↓, McGraw-Hill.

␈↓ ↓H␈↓␈↓αBaker,␈αHenry␈α
G.␈α[1978]␈↓:␈α "List␈α
Processing␈αin␈αReal␈α
TIme␈αon␈αa␈α
Serial␈αComputer",␈α␈↓↓Communications␈α
of
␈↓ ↓H␈↓↓␈↓ αλthe ACM␈↓ ␈↓α21␈↓, pp. 280-294.

␈↓ ↓H␈↓        Description␈α
and␈αanalysis␈α
of␈αa␈α
list␈α
processing␈αsystem␈α
that␈αcontinuously␈α
reclaims␈αgarbage␈α
while
␈↓ ↓H␈↓␈↓ αλlinearizing and compacting space in use.

␈↓ ↓H␈↓␈↓αBoyer, Robert S. and J. Strather Moore [1978]␈↓: ␈↓↓A Computational Logic␈↓  manuscript.

␈↓ ↓H␈↓␈↓αBrudno,␈α∞A.␈α
L.␈α∞[1963]␈↓:␈α∞ "Bounds␈α
and␈α∞Valuations␈α
for␈α∞Shortening␈α∞the␈α
Scanning␈α∞of␈α∞Variations",␈α
[in
␈↓ ↓H␈↓␈↓ αλRussian], ␈↓↓Problemy Kibernetiki␈↓ ␈↓α10␈↓ pp.141-150.

␈↓ ↓H␈↓        First published account of the αβ algorithm.

␈↓ ↓H␈↓␈↓αCartwright,␈α∪Robert␈α∪[1977]␈↓:␈α∪ "Practical␈α∪Formal␈α∪Semantic␈α∪Defintion␈α∪and␈α∪Verification␈α∩Systems,"
␈↓ ↓H␈↓␈↓ αλPh.D. thesis, Computer Science Department, Stanford University, Stanford, Ca..

␈↓ ↓H␈↓␈↓αFilman,␈α⊃R.␈α⊃E.␈α⊃and␈α⊃R.␈α⊃W.␈α⊃Weyhrauch␈α⊃[1976]␈↓:␈α⊃␈↓↓A␈α⊃FOL␈α⊃Primer␈↓,␈α⊃Stanford␈α⊃Artificial␈α⊂Intelligence
␈↓ ↓H␈↓␈↓ αλLaboratory Memo AIM-288.

␈↓ ↓H␈↓        An excellent introduction to using the automatic first order logic proof checker FOL.

␈↓ ↓H␈↓␈↓αGordon,␈α∩M.␈α∩[1975]␈↓:␈α∩ "Operational␈α∩Reasoning␈α∩and␈α∩Denotational␈α∩Semantics",␈α∩Stanford␈α⊃Artificial
␈↓ ↓H␈↓␈↓ αλIntelligence Laboratory Memo, AIM-264.

␈↓ ↓H␈↓␈↓αKnuth,␈α∩D.␈α⊃E.␈α∩[1968a]␈↓:␈α⊃ ␈↓↓The␈α∩Art␈α⊃of␈α∩Computer␈α⊃Programming,␈α∩Vol.␈α⊃I:␈α∩Fundamental␈α⊃Algorithms␈↓.
␈↓ ↓H␈↓␈↓ αλAddison-Wesley.

␈↓ ↓H␈↓        Section ␈↓π∞␈↓2.3.5 contains a discussion of list structures and garbage collection algorithms.

␈↓ ↓H␈↓␈↓αKnuth,␈α⊃D.␈α∩E.␈α⊃[1968b]␈↓:␈α⊃ ␈↓↓The␈α∩Art␈α⊃of␈α∩Computer␈α⊃Programming,␈α⊃Vol.␈α∩III:␈α⊃Sorting␈α∩and␈α⊃Searching␈↓.
␈↓ ↓H␈↓␈↓ αλAddison-Wesley.

␈↓ ↓H␈↓        Section ␈↓π∞␈↓6.4 contains a discussion of hashing.

␈↓ ↓H␈↓␈↓αKnuth,␈αD.␈αE.␈αand␈αR.␈αMoore␈α[1975]␈↓:␈α "An␈αAnalysis␈αof␈αAlpha-Beta␈αPruning",␈α␈↓↓Artificial␈αIntelligence␈↓,
␈↓ ↓H␈↓␈↓ αλ␈↓α6␈↓.

␈↓ ↓H␈↓        A history, description, and analysis of the αβ algorithm.

␈↓ ↓H␈↓␈↓αManna, Zohar [1974]␈↓: ␈↓↓Mathematical Theory of Computation␈↓, McGraw-Hill.

␈↓ ↓H␈↓        Chapter␈α2␈αcontains␈αa␈αdiscussion␈αof␈αfirst␈α
order␈αlogic␈αand␈αof␈αthe␈α␈↓↓Natural deduction␈↓␈αmethod␈α
of
␈↓ ↓H␈↓␈↓ αλproof.
␈↓ ↓H␈↓ii␈↓ ¬KBIBLIOGRAPHY␈↓ H


␈↓ ↓H␈↓        Chapter 5 contains a discussion of functionals and fixedpoints.

␈↓ ↓H␈↓        The␈α
book␈αalso␈α
contains␈α
a␈αdiscussion␈α
of␈αstructural␈α
induction␈α
and␈αother␈α
methods␈α
of␈αproving
␈↓ ↓H␈↓␈↓ αλproperties of programs.

␈↓ ↓H␈↓␈↓αMcCarthy,␈α
John␈α[1962a]␈↓:␈α
"Computer␈α
Programs␈αfor␈α
Checking␈αMathematical␈α
Proofs",␈α
␈↓↓Proc.␈αSymp.
␈↓ ↓H␈↓↓␈↓ αλPure Math.␈↓ Vol.5 , Amer. Amth. Soc., Providence, R. I., pp219-227.

␈↓ ↓H␈↓␈↓αMcCarthy,␈α_John␈α↔[1962b]␈↓:␈α_ "Towards␈α↔a␈α_Mathematical␈α↔Science␈α_of␈α↔Computation,"␈α_in␈α_C.␈α↔M.
␈↓ ↓H␈↓␈↓ αλPopplewell␈α
(ed.),␈α
␈↓↓Information␈α
Processing,␈α
Proceedings␈α
of␈α
IFIP␈α
Congress␈α
62␈↓,␈α∞pp21-28,␈α
North
␈↓ ↓H␈↓␈↓ αλHolland Publishing Company, Amsterdam.

␈↓ ↓H␈↓        Contains␈αdiscussions␈αof␈αtransforming␈αFlow␈αChart␈αprograms␈αinto␈αrecursive␈αprograms␈αand␈αof
␈↓ ↓H␈↓␈↓ αλthe Abstract Syntax of programming languages.

␈↓ ↓H␈↓␈↓αMcCarthy,␈αJohn␈α
[1963]␈↓:␈α"A␈αBasis␈α
for␈αa␈αMathematical␈α
Theory␈αof␈αComputation",␈α
in␈αP.␈αBraffort␈α
and
␈↓ ↓H␈↓␈↓ αλD.␈αHirschberg␈α(eds.),␈α␈↓↓Computer␈αProgramming␈αand␈αFormal␈αSystems␈↓,␈αpp.␈α33-70.␈αNorth-Holland
␈↓ ↓H␈↓␈↓ αλPublishing Company, Amsterdam.

␈↓ ↓H␈↓        Contains␈α⊃a␈α⊃complete␈α⊃discussion␈α⊃of␈α⊃properties␈α⊃of␈α⊃conditional␈α⊃forms,␈α⊃including␈α⊃cannonical
␈↓ ↓H␈↓␈↓ αλforms.  Also discusses computable functionals and recursion induction.

␈↓ ↓H␈↓␈↓αMcCarthy,␈α∂John␈α∂[1964]␈↓:␈α∂ "A␈α∞Formal␈α∂Description␈α∂of␈α∂a␈α∂Subset␈α∞of␈α∂Algol",␈α∂␈↓↓Proc.␈α∂Conf.␈α∂on␈α∞Formal
␈↓ ↓H␈↓↓␈↓ αλLanguage Description Languages␈↓, Vienna.

␈↓ ↓H␈↓␈↓αMcCarthy,␈α∂John␈α∂[1978a]␈↓:␈α∂ "History␈α∂of␈α∂LISP",␈α∂␈↓↓Proceedings␈α∂of␈α∂the␈α∂ACM␈α∂conference␈α∂on␈α∂History␈α∂of
␈↓ ↓H␈↓↓␈↓ αλProgramming Languages␈↓, 1978.

␈↓ ↓H␈↓␈↓αMcCarthy,␈α_John␈α↔[1978b]␈↓:␈α_ "Representation␈α_of␈α↔Recursive␈α_Programs␈α↔in␈α_First␈α_Order␈α↔Logic",
␈↓ ↓H␈↓␈↓ αλ␈↓↓Proceedings␈αthe␈α
International␈αConference␈α
on␈αMathematical␈α
Studies␈αof␈α
Information␈αProcessing␈↓,
␈↓ ↓H␈↓␈↓ αλKyoto Japan, 1978.

␈↓ ↓H␈↓␈↓αMcCarthy,␈α→John␈α_and␈α→James␈α→Painter␈α_[1967]␈↓:␈α→"Correctness␈α→of␈α_a␈α→Compiler␈α→for␈α_Arithmetic
␈↓ ↓H␈↓␈↓ αλExpressions",␈α␈↓↓Proceeding␈αof␈αSymposia␈αin␈αApplied␈α Mathematics␈↓,␈αVol.␈α19,␈αJ.␈αT.␈αSchwartz␈α(ed.),
␈↓ ↓H␈↓␈↓ αλAmerican Mathematical Society, pp 33-41.

␈↓ ↓H␈↓␈↓αMoszkowski, B. [1978]␈↓: Stanford Artificial Intelligence Laboratory Memo, (to appear).

␈↓ ↓H␈↓␈↓αNilsson, N. J. [1971]␈↓:  ␈↓↓Problem Solving Methods in Artificial Intelligence␈↓, McGraw-Hill.

␈↓ ↓H␈↓␈↓αPainter,␈α
James␈α
[1967]␈↓:␈α "Semantic␈α
Correctness␈α
of␈αa␈α
Compiler␈α
for␈αan␈α
Algol-like␈α
Language",␈αPh.D.
␈↓ ↓H␈↓␈↓ αλthesis, Computer Science Department, Stanford University, Stanford, Ca..

␈↓ ↓H␈↓␈↓αSteele,␈α
G.L.␈α
[1978]␈↓:␈α
"RABBIT:␈α
A␈α
Compiler␈αfor␈α
SCHEME"␈α
M.S.␈α
 thesis,␈α
Department␈α
of␈αElectrical
␈↓ ↓H␈↓␈↓ αλEngineering and Computer Science,MIT.␈↓α

␈↓ ↓H␈↓αSussman,␈αG.J.␈α
and␈αG.L.␈α
Steele[1978]:␈α"The␈α
revised␈αreport␈α
on␈αSCHEME,␈α
a␈αDialect␈α
of␈αLISP",␈α
MIT-
␈↓ ↓H␈↓α␈↓ αλAI Memo 452.

␈↓ ↓H␈↓αVuillemin,␈α∩J.␈α∩[1973]:␈α∩"Proof␈α∩Techniques␈α∩for␈α∩Recursive␈α∩Programs,"␈α∩Ph.D.␈α∩ thesis,␈α∩Computer
␈↓ ↓H␈↓α␈↓ αλScience Department, Stanford University, Stanford, Ca..
␈↓ ↓H␈↓␈↓ ¬KBIBLIOGRAPHY␈↓ -iii


␈↓ ↓H␈↓αWeyhrauch,␈α⊗R.␈α⊗W.␈α⊗[1977]:␈α⊗ "A␈α⊗users␈α⊗manual␈α⊗for␈α⊗FOL",␈α⊗Stanford␈α↔Artificial␈α⊗Intelligence
␈↓ ↓H␈↓α␈↓ αλLaboratory Memo, AIM-235.1.

␈↓ ↓H␈↓α        A manual for the automatic proof checker FOL.

␈↓ ↓H␈↓αWinston, P. H. [1977]:  ␈↓↓Artificial Intelligence␈↓α,  Addison-Wesley.
␈↓ ↓H␈↓␈↓ εH␈↓ ?i


␈↓ ↓H␈↓α␈↓ ¬uAppendix A

␈↓ ↓H␈↓α␈↓ ¬\LISP Compilers




␈↓ ↓H␈↓1.  ␈↓αLCOM0: listing of MACLISP version.␈↓


␈↓ ↓H␈↓ε(DECLARE (SETQ NO-DISK-HACKS T))
␈↓ ↓H␈↓ε(DECLARE (READ))

␈↓ ↓H␈↓ε(DEFPROP LC0FNS
␈↓ ↓H␈↓ε (LC0FNS COMPL COMP PRUP MKPUSH COMPEXP COMPLIS LOADAC COMCOND COMBOOL COMPANDOR)
␈↓ ↓H␈↓εVALUE)

␈↓ ↓H␈↓ε(DEFPROP COMPL
␈↓ ↓H␈↓ε (LAMBDA(FILE)
␈↓ ↓H␈↓ε  (UWRITE)
␈↓ ↓H␈↓ε  (APPLY (QUOTE EREAD) FILE)
␈↓ ↓H␈↓ε  (SELECT-DISK-INPUT
␈↓ ↓H␈↓ε   (READ-UNTIL-EOF
␈↓ ↓H␈↓ε    WITH
␈↓ ↓H␈↓ε    Z
␈↓ ↓H␈↓ε    DO
␈↓ ↓H␈↓ε    (COND ((OR (EQ (CAR Z) (QUOTE DEFUN)) (AND (EQ (CAR Z) (QUOTE DEFPROP)) (EQ (CADDDR Z) (QUOTE EXPR))))
␈↓ ↓H␈↓ε           (PROG (PROG)
␈↓ ↓H␈↓ε                 (SETQ PROG
␈↓ ↓H␈↓ε                       (COND ((EQ (CAR Z) (QUOTE DEFUN)) (COMP (CADR Z) (CADDR Z) (CADDDR Z)))
␈↓ ↓H␈↓ε                             (T (COMP (CADR Z) (CADR (CADDR Z)) (CADDR (CADDR Z))))))
␈↓ ↓H␈↓ε                 (UNSELECT-TTY (SELECT-DISK-OUTPUT (MAPC (FUNCTION PRINT) PROG)))
␈↓ ↓H␈↓ε                 (PRINT (LIST (CADR Z) (LENGTH PROG)))))
␈↓ ↓H␈↓ε          (T (UNSELECT-TTY (SELECT-DISK-OUTPUT (PRINT Z))))))
␈↓ ↓H␈↓ε   (APPLY (QUOTE UFILE) (LIST (CAR FILE) (QUOTE LAP)))
␈↓ ↓H␈↓ε   (QUOTE ENDCOMP)))
␈↓ ↓H␈↓εFEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMP
␈↓ ↓H␈↓ε (LAMBDA(FN VARS EXP)
␈↓ ↓H␈↓ε  ((LAMBDA(N)
␈↓ ↓H␈↓ε    (APPEND (LIST (LIST (QUOTE LAP) FN (QUOTE SUBR)))
␈↓ ↓H␈↓ε            (MKPUSH N 1)
␈↓ ↓H␈↓ε            (COMPEXP EXP (MINUS N) (PRUP VARS 1))
␈↓ ↓H␈↓ε            (LIST (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE %) 0 0 N N)))
␈↓ ↓H␈↓ε            (QUOTE ((POPJ P) NIL))))
␈↓ ↓H␈↓ε   (LENGTH VARS)))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP PRUP
␈↓ ↓H␈↓ε (LAMBDA (VARS N) (COND ((NULL VARS) NIL) (T (CONS (CONS (CAR VARS) N) (PRUP (CDR VARS) (ADD1 N))))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP MKPUSH
␈↓ ↓H␈↓ε (LAMBDA (N M) (COND ((LESSP N M) NIL) (T (CONS (LIST (QUOTE PUSH) (QUOTE P) M) (MKPUSH N (ADD1 M))))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPEXP
␈↓ ↓H␈↓ε (LAMBDA(EXP M VPR)
␈↓ ↓H␈↓ε  (COND ((NULL EXP) (QUOTE ((MOVEI 1 0))))
␈↓ ↓H␈↓ε        ((EQ EXP T) (QUOTE ((MOVEI 1 (QUOTE T)))))
␈↓ ↓H␈↓ε        ((NUMBERP EXP) (LIST (LIST (QUOTE MOVEI) 1 (LIST (QUOTE QUOTE) EXP))))
␈↓ ↓H␈↓ε        ((ATOM EXP) (LIST (LIST (QUOTE MOVE) 1 (PLUS M (CDR (ASSOC EXP VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε        ((OR (EQ (CAR EXP) (QUOTE AND)) (EQ (CAR EXP) (QUOTE OR)) (EQ (CAR EXP) (QUOTE NOT)))
␈↓ ↓H␈↓ε         ((LAMBDA(L1 L2)
␈↓ ↓H␈↓ε           (APPEND (COMBOOL EXP M L1 NIL VPR)
␈↓ ↓H␈↓ii␈↓ ¬pAppendix  A␈↓ H


␈↓ ↓H␈↓ε                   (LIST (QUOTE (MOVEI 1 (QUOTE T))) (LIST (QUOTE JRST) 0 L2) L1 (QUOTE (MOVEI 1 0)) L2)))
␈↓ ↓H␈↓ε          (GENSYM)
␈↓ ↓H␈↓ε          (GENSYM)))
␈↓ ↓H␈↓ε        ((EQ (CAR EXP) (QUOTE COND)) (COMCOND (CDR EXP) M (GENSYM) VPR))
␈↓ ↓H␈↓ε        ((EQ (CAR EXP) (QUOTE QUOTE)) (LIST (LIST (QUOTE MOVEI) 1 EXP)))
␈↓ ↓H␈↓ε        ((ATOM (CAR EXP))
␈↓ ↓H␈↓ε         ((LAMBDA(N)
␈↓ ↓H␈↓ε           (APPEND (COMPLIS (CDR EXP) M VPR)
␈↓ ↓H␈↓ε                   (LOADAC (DIFFERENCE 1 N) 1)
␈↓ ↓H␈↓ε                   (LIST (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE %) 0 0 N N)))
␈↓ ↓H␈↓ε                   (LIST (LIST (QUOTE CALL) N (LIST (QUOTE QUOTE) (CAR EXP))))))
␈↓ ↓H␈↓ε          (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ε        ((EQ (CAAR EXP) (QUOTE LAMBDA))
␈↓ ↓H␈↓ε         ((LAMBDA(N)
␈↓ ↓H␈↓ε           (APPEND (COMPLIS (CDR EXP) M VPR)
␈↓ ↓H␈↓ε                   (COMPEXP (CADDAR EXP) 
␈↓ ↓H␈↓ε                            (DIFFERENCE M N) 
␈↓ ↓H␈↓ε                            (APPEND (PRUP (CADAR EXP) (DIFFERENCE 1 M)) VPR))
␈↓ ↓H␈↓ε                   (LIST (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE %) 0 0 N N)))))
␈↓ ↓H␈↓ε          (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ε        (T NIL)))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPLIS
␈↓ ↓H␈↓ε (LAMBDA(U M VPR)
␈↓ ↓H␈↓ε  (COND ((NULL U) NIL)
␈↓ ↓H␈↓ε        (T (APPEND (COMPEXP (CAR U) M VPR) (QUOTE ((PUSH P 1))) (COMPLIS (CDR U) (SUB1 M) VPR)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP LOADAC
␈↓ ↓H␈↓ε (LAMBDA(N K)
␈↓ ↓H␈↓ε  (COND ((GREATERP N 0) NIL) (T (CONS (LIST (QUOTE MOVE) K N (QUOTE P)) (LOADAC (ADD1 N) (ADD1 K))))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMCOND
␈↓ ↓H␈↓ε (LAMBDA(U M L VPR)
␈↓ ↓H␈↓ε  (COND ((NULL U) (LIST L))
␈↓ ↓H␈↓ε        (T
␈↓ ↓H␈↓ε         ((LAMBDA(L1)
␈↓ ↓H␈↓ε           (APPEND (COMBOOL (CAAR U) M L1 NIL VPR)
␈↓ ↓H␈↓ε                   (COMPEXP (CADAR U) M VPR)
␈↓ ↓H␈↓ε                   (LIST (LIST (QUOTE JRST) 0 L) L1)
␈↓ ↓H␈↓ε                   (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ε          (GENSYM)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMBOOL
␈↓ ↓H␈↓ε (LAMBDA(P M L FLG VPR)
␈↓ ↓H␈↓ε  (COND ((ATOM P) 
␈↓ ↓H␈↓ε         (APPEND (COMPEXP P M VPR) (LIST (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE))) 1 L))))
␈↓ ↓H␈↓ε        ((EQ (CAR P) (QUOTE AND))
␈↓ ↓H␈↓ε         (COND ((NOT FLG) (COMPANDOR (CDR P) M L NIL VPR))
␈↓ ↓H␈↓ε               (T
␈↓ ↓H␈↓ε                ((LAMBDA(L1)
␈↓ ↓H␈↓ε                  (APPEND (COMPANDOR (CDR P) M L1 NIL VPR) (LIST (LIST (QUOTE JRST) 0 L)) (LIST L1)))
␈↓ ↓H␈↓ε                 (GENSYM)))))
␈↓ ↓H␈↓ε        ((EQ (CAR P) (QUOTE OR))
␈↓ ↓H␈↓ε         (COND (FLG (COMPANDOR (CDR P) M L T VPR))
␈↓ ↓H␈↓ε               (T
␈↓ ↓H␈↓ε                ((LAMBDA (L1) 
␈↓ ↓H␈↓ε                  (APPEND (COMPANDOR (CDR P) M L1 T VPR) (LIST (LIST (QUOTE JRST) 0 L)) (LIST L1)))
␈↓ ↓H␈↓ε                 (GENSYM)))))
␈↓ ↓H␈↓ε        ((EQ (CAR P) (QUOTE NOT)) (COMBOOL (CADR P) M L (NOT FLG) VPR))
␈↓ ↓H␈↓ε        (T (APPEND (COMPEXP P M VPR) (LIST (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE))) 1 L))))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPANDOR
␈↓ ↓H␈↓ε (LAMBDA(U M L FLG VPR)
␈↓ ↓H␈↓ε  (COND ((NULL U) NIL) (T (APPEND (COMBOOL (CAR U) M L FLG VPR) (COMPANDOR (CDR U) M L FLG VPR)))))
␈↓ ↓H␈↓␈↓ ¬pAppendix  A␈↓ -iii


␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓iv␈↓ ¬pAppendix  A␈↓ H


␈↓ ↓H␈↓2.  ␈↓αLCOM4: listing of MACLISP version.␈↓


␈↓ ↓H␈↓ε(DECLARE (SETQ NO-DISK-HACKS T))
␈↓ ↓H␈↓ε(DECLARE (READ))

␈↓ ↓H␈↓ε(DEFPROP COMPFCNS
␈↓ ↓H␈↓ε (COMPFCNS COMPL
␈↓ ↓H␈↓ε           COMP
␈↓ ↓H␈↓ε           SUBSTACK
␈↓ ↓H␈↓ε           PRUP
␈↓ ↓H␈↓ε           MKPUSH
␈↓ ↓H␈↓ε           COMPEXP
␈↓ ↓H␈↓ε           STACKUP
␈↓ ↓H␈↓ε           CCCHAIN
␈↓ ↓H␈↓ε           COMPC
␈↓ ↓H␈↓ε           COMCOND
␈↓ ↓H␈↓ε           COMPLISA
␈↓ ↓H␈↓ε           CCOUNT
␈↓ ↓H␈↓ε           LOADAC
␈↓ ↓H␈↓ε           COMPLIS
␈↓ ↓H␈↓ε           CLASSIFY
␈↓ ↓H␈↓ε           CLASS1
␈↓ ↓H␈↓ε           CLASS2
␈↓ ↓H␈↓ε           MKJRST
␈↓ ↓H␈↓ε           COMBOOL
␈↓ ↓H␈↓ε           COMPANDOR
␈↓ ↓H␈↓ε           COMPANDOR1
␈↓ ↓H␈↓ε           FLAT)
␈↓ ↓H␈↓εVALUE)

␈↓ ↓H␈↓ε(DEFPROP COMPL
␈↓ ↓H␈↓ε (LAMBDA(FILE)
␈↓ ↓H␈↓ε  (UWRITE)
␈↓ ↓H␈↓ε  (APPLY (QUOTE EREAD) FILE)
␈↓ ↓H␈↓ε  (SELECT-DISK-INPUT
␈↓ ↓H␈↓ε   (READ-UNTIL-EOF
␈↓ ↓H␈↓ε    WITH
␈↓ ↓H␈↓ε    Z
␈↓ ↓H␈↓ε    DO
␈↓ ↓H␈↓ε    (COND ((OR (EQ (CAR Z) (QUOTE DEFUN)) (AND (EQ (CAR Z) (QUOTE DEFPROP)) (EQ (CADDDR Z) (QUOTE EXPR))))
␈↓ ↓H␈↓ε           (PROG (PROG)
␈↓ ↓H␈↓ε                 (SETQ PROG
␈↓ ↓H␈↓ε                       (COND ((EQ (CAR Z) (QUOTE DEFUN)) (COMP (CADR Z) (CADDR Z) (CADDDR Z)))
␈↓ ↓H␈↓ε                             (T (COMP (CADR Z) (CADR (CADDR Z)) (CADDR (CADDR Z))))))
␈↓ ↓H␈↓ε                 (UNSELECT-TTY (SELECT-DISK-OUTPUT (MAPC (FUNCTION PRINT) PROG)))
␈↓ ↓H␈↓ε                 (PRINT (LIST (CADR Z) (LENGTH PROG)))))
␈↓ ↓H␈↓ε          (T (UNSELECT-TTY (SELECT-DISK-OUTPUT (PRINT Z))))))
␈↓ ↓H␈↓ε   (APPLY (QUOTE UFILE) (LIST (CAR FILE) (QUOTE LAP)))
␈↓ ↓H␈↓ε   (QUOTE ENDCOMP)))
␈↓ ↓H␈↓εFEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMP
␈↓ ↓H␈↓ε (LAMBDA(FN VARS EXP)
␈↓ ↓H␈↓ε  ((LAMBDA(VPR N)
␈↓ ↓H␈↓ε    (FLAT (LIST (LIST (LIST (QUOTE LAP) FN (QUOTE SUBR)))
␈↓ ↓H␈↓ε                (MKPUSH N 1)
␈↓ ↓H␈↓ε                (COMPEXP EXP (MINUS N) VPR)
␈↓ ↓H␈↓ε                (SUBSTACK N)
␈↓ ↓H␈↓ε                (QUOTE ((POPJ P) (LABEL NIL))))
␈↓ ↓H␈↓ε          NIL))
␈↓ ↓H␈↓ε   (PRUP VARS 1)
␈↓ ↓H␈↓ε   (LENGTH VARS)))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP SUBSTACK
␈↓ ↓H␈↓ε (LAMBDA (N) (COND ((= N 0) NIL) (T (LIST (LIST (QUOTE SUB) (QUOTE P) (LIST (QUOTE %) 0 0 N N))))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓␈↓ ¬pAppendix  A␈↓ 7v


␈↓ ↓H␈↓ε(DEFPROP PRUP
␈↓ ↓H␈↓ε (LAMBDA (VARS N) (COND ((NULL VARS) NIL) (T (CONS (CONS (CAR VARS) N) (PRUP (CDR VARS) (ADD1 N))))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP MKPUSH
␈↓ ↓H␈↓ε (LAMBDA (N M) (COND ((LESSP N M) NIL) (T (CONS (LIST (QUOTE PUSH) (QUOTE P) M) (MKPUSH N (ADD1 M))))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPEXP
␈↓ ↓H␈↓ε (LAMBDA(EXP M VPR)
␈↓ ↓H␈↓ε  (COND ((NULL EXP) (QUOTE ((MOVEI 1 0))))
␈↓ ↓H␈↓ε        ((OR (EQ EXP T) (NUMBERP EXP)) (LIST (LIST (QUOTE MOVEI) 1 (LIST (QUOTE QUOTE) EXP))))
␈↓ ↓H␈↓ε        ((ATOM EXP) (LIST (LIST (QUOTE MOVE) 1 (PLUS M (CDR (ASSOC EXP VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε        ((EQ (CAR EXP) (QUOTE CAR))
␈↓ ↓H␈↓ε         (COND ((ATOM (CADR EXP)) 
␈↓ ↓H␈↓ε                (LIST (LIST (QUOTE HLRZ) 1 (QUOTE @)(PLUS M (CDR (ASSOC (CADR EXP) VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε               (T (LIST (COMPEXP (CADR EXP) M VPR) (QUOTE ((HLRZ 1 @ 1)))))))
␈↓ ↓H␈↓ε        ((EQ (CAR EXP) (QUOTE CDR))
␈↓ ↓H␈↓ε         (COND ((ATOM (CADR EXP)) 
␈↓ ↓H␈↓ε                (LIST (LIST (QUOTE HRRZ) 1 (QUOTE @)(PLUS M (CDR (ASSOC (CADR EXP) VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε               (T (LIST (COMPEXP (CADR EXP) M VPR) (QUOTE ((HRRZ 1 @ 1)))))))
␈↓ ↓H␈↓ε        ((OR (EQ (CAR EXP) (QUOTE AND))
␈↓ ↓H␈↓ε             (EQ (CAR EXP) (QUOTE OR))
␈↓ ↓H␈↓ε             (EQ (CAR EXP) (QUOTE NOT))
␈↓ ↓H␈↓ε             (EQ (CAR EXP) (QUOTE EQ)))
␈↓ ↓H␈↓ε         ((LAMBDA(L1 L2)
␈↓ ↓H␈↓ε           (LIST (COMBOOL EXP M L1 NIL VPR)
␈↓ ↓H␈↓ε                 (LIST (QUOTE (MOVEI 1 (QUOTE T)))
␈↓ ↓H␈↓ε                       (LIST (QUOTE JRST) 0 L2)
␈↓ ↓H␈↓ε                       (LIST (QUOTE LABEL) L1)
␈↓ ↓H␈↓ε                       (QUOTE (MOVEI 1 0))
␈↓ ↓H␈↓ε                       (LIST (QUOTE LABEL) L2))))
␈↓ ↓H␈↓ε          (GENSYM)
␈↓ ↓H␈↓ε          (GENSYM)))
␈↓ ↓H␈↓ε        ((EQ (CAR EXP) (QUOTE COND)) (COMCOND (CDR EXP) M (GENSYM) VPR))
␈↓ ↓H␈↓ε        ((EQ (CAR EXP) (QUOTE QUOTE)) (LIST (LIST (QUOTE MOVEI) 1 EXP)))
␈↓ ↓H␈↓ε        ((ATOM (CAR EXP))
␈↓ ↓H␈↓ε         (LIST (COMPLISA (CDR EXP) M VPR)
␈↓ ↓H␈↓ε               (LIST (LIST (QUOTE CALL) (LENGTH (CDR EXP)) (LIST (QUOTE QUOTE) (CAR EXP))))))
␈↓ ↓H␈↓ε        ((EQ (CAAR EXP) (QUOTE LAMBDA))
␈↓ ↓H␈↓ε         ((LAMBDA(N)
␈↓ ↓H␈↓ε           (LIST (STACKUP (CDR EXP) M VPR)
␈↓ ↓H␈↓ε                 (COMPEXP (CADDAR EXP) (DIFFERENCE M N) (APPEND (PRUP (CADAR EXP) (DIFFERENCE 1 M)) VPR))
␈↓ ↓H␈↓ε                 (SUBSTACK N)))
␈↓ ↓H␈↓ε          (LENGTH (CDR EXP))))
␈↓ ↓H␈↓ε        (T NIL)))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP STACKUP
␈↓ ↓H␈↓ε (LAMBDA(U M VPR)
␈↓ ↓H␈↓ε  (COND ((NULL U) NIL) 
␈↓ ↓H␈↓ε        (T (LIST (COMPEXP (CAR U) M VPR) (QUOTE ((PUSH P 1))) (STACKUP (CDR U) (SUB1 M) VPR)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP CCCHAIN
␈↓ ↓H␈↓ε (LAMBDA(EXP)
␈↓ ↓H␈↓ε  (AND (OR (EQ (CAR EXP) (QUOTE CAR)) (EQ (CAR EXP) (QUOTE CDR))) 
␈↓ ↓H␈↓ε       (OR (ATOM (CADR EXP)) (CCCHAIN (CADR EXP)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPC
␈↓ ↓H␈↓ε (LAMBDA(EXP N2 M VPR)
␈↓ ↓H␈↓ε  (COND ((ATOM EXP) (ERROR (QUOTE COMPC)))
␈↓ ↓H␈↓ε        ((EQ (CAR EXP) (QUOTE CAR))
␈↓ ↓H␈↓ε         (COND ((ATOM (CADR EXP))
␈↓ ↓H␈↓ε                (LIST (LIST (QUOTE HLRZ) N2 (QUOTE @)(PLUS M (CDR (ASSOC (CADR EXP) VPR))) (QUOTE P))))
␈↓ ↓H␈↓ε               (T (CONS (LIST (QUOTE HLRZ) N2 (QUOTE @) N2) (COMPC (CADR EXP) N2 M VPR)))))
␈↓ ↓H␈↓ε        ((ATOM (CADR EXP)) 
␈↓ ↓H␈↓ε         (LIST (LIST (QUOTE HRRZ) (QUOTE @) N2 (PLUS M (CDR (ASSOC (CADR EXP) VPR))) (QUOTE P))))
␈↓ ↓H␈↓vi␈↓ ¬pAppendix  A␈↓ H


␈↓ ↓H␈↓ε        (T (CONS (LIST (QUOTE HRRZ) N2 (QUOTE @) N2) (COMPC (CADR EXP) N2 M VPR)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMCOND
␈↓ ↓H␈↓ε (LAMBDA(U M L VPR)
␈↓ ↓H␈↓ε  (COND ((NULL U) (LIST (LIST (QUOTE LABEL) L)))
␈↓ ↓H␈↓ε        ((AND (NOT (ATOM (CAAR U))) (EQ (CAAAR U) (QUOTE NULL)) (NULL (CADAR U)))
␈↓ ↓H␈↓ε         (LIST (COMPEXP (CADAAR U) M VPR) (LIST (LIST (QUOTE JUMPE) 1 L)) (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ε        ((EQ (CAAR U) T) (LIST (COMPEXP (CADAR U) M VPR) (LIST (LIST (QUOTE LABEL) L))))
␈↓ ↓H␈↓ε        (T
␈↓ ↓H␈↓ε         ((LAMBDA(L1)
␈↓ ↓H␈↓ε           (LIST (COMBOOL (CAAR U) M L1 NIL VPR)
␈↓ ↓H␈↓ε                 (COMPEXP (CADAR U) M VPR)
␈↓ ↓H␈↓ε                 (LIST (LIST (QUOTE JRST) 0 L) (LIST (QUOTE LABEL) L1))
␈↓ ↓H␈↓ε                 (COMCOND (CDR U) M L VPR)))
␈↓ ↓H␈↓ε          (GENSYM)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPLISA
␈↓ ↓H␈↓ε (LAMBDA(U M VPR)
␈↓ ↓H␈↓ε  ((LAMBDA(Z)
␈↓ ↓H␈↓ε    (LIST (COMPLIS Z M 1 VPR)
␈↓ ↓H␈↓ε          (LOADAC Z (DIFFERENCE 1 (CCOUNT Z)) 1 (DIFFERENCE M (CCOUNT Z)) VPR)
␈↓ ↓H␈↓ε          (SUBSTACK (CCOUNT Z))))
␈↓ ↓H␈↓ε   (CLASSIFY U)))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP CCOUNT
␈↓ ↓H␈↓ε (LAMBDA (Z) (COND ((NULL Z) 0) ((= (CAAR Z) 4) (ADD1 (CCOUNT (CDR Z)))) (T (CCOUNT (CDR Z)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP LOADAC
␈↓ ↓H␈↓ε (LAMBDA(Z M2 N2 M VPR)
␈↓ ↓H␈↓ε  (COND ((NULL Z) NIL)
␈↓ ↓H␈↓ε        ((= (CAAR Z) 1)
␈↓ ↓H␈↓ε         (CONS (LIST (QUOTE MOVE) N2 (PLUS M (CDR (ASSOC (CDAR Z) VPR))) (QUOTE P))
␈↓ ↓H␈↓ε               (LOADAC (CDR Z) M2 (ADD1 N2) M VPR)))
␈↓ ↓H␈↓ε        ((= (CAAR Z) 0)
␈↓ ↓H␈↓ε         (CONS (LIST (QUOTE MOVEI) N2 (LIST (QUOTE QUOTE) (CDAR Z))) (LOADAC (CDR Z) M2 (ADD1 N2) M VPR)))
␈↓ ↓H␈↓ε        ((= (CAAR Z) 2) (CONS (LIST (QUOTE MOVEI) N2 (CDAR Z)) (LOADAC (CDR Z) M2 (ADD1 N2) M VPR)))
␈↓ ↓H␈↓ε        ((= (CAAR Z) 3) (LIST (REVERSE (COMPC (CDAR Z) N2 M VPR)) (LOADAC (CDR Z) M2 (ADD1 N2) M VPR)))
␈↓ ↓H␈↓ε        ((= (CAAR Z) 5) (LOADAC (CDR Z) 1 (ADD1 N2) M VPR))
␈↓ ↓H␈↓ε        (T (CONS (LIST (QUOTE MOVE) N2 M2 (QUOTE P)) (LOADAC (CDR Z) (ADD1 M2) (ADD1 N2) M VPR)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPLIS
␈↓ ↓H␈↓ε (LAMBDA(Z M K VPR)
␈↓ ↓H␈↓ε  (COND ((NULL Z) NIL)
␈↓ ↓H␈↓ε        ((= (CAAR Z) 4)
␈↓ ↓H␈↓ε         (LIST (COMPEXP (CDAR Z) M VPR) (QUOTE ((PUSH P 1))) (COMPLIS (CDR Z) (SUB1 M) (ADD1 K) VPR)))
␈↓ ↓H␈↓ε        ((= (CAAR Z) 5) 
␈↓ ↓H␈↓ε         (LIST (COMPEXP (CDAR Z) M VPR) (COND ((= K 1) NIL) (T (LIST (LIST (QUOTE MOVE) K 1))))))
␈↓ ↓H␈↓ε        (T (COMPLIS (CDR Z) M (ADD1 K) VPR))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP CLASSIFY
␈↓ ↓H␈↓ε (LAMBDA (U) (CLASS2 (CLASS1 U NIL) NIL T))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP CLASS1
␈↓ ↓H␈↓ε (LAMBDA(U V)
␈↓ ↓H␈↓ε  (COND ((NULL U) V)
␈↓ ↓H␈↓ε        ((ATOM (CAR U))
␈↓ ↓H␈↓ε         (COND ((OR (EQUAL (CAR U) NIL) (EQUAL (CAR U) T) (NUMBERP (CAR U)))
␈↓ ↓H␈↓ε                (CLASS1 (CDR U) (CONS (CONS 0 (CAR U)) V)))
␈↓ ↓H␈↓ε               (T (CLASS1 (CDR U) (CONS (CONS 1 (CAR U)) V)))))
␈↓ ↓H␈↓ε        ((EQUAL (CAAR U) (QUOTE QUOTE)) (CLASS1 (CDR U) (CONS (CONS 2 (CAR U)) V)))
␈↓ ↓H␈↓ε        ((CCCHAIN (CAR U)) (CLASS1 (CDR U) (CONS (CONS 3 (CAR U)) V)))
␈↓ ↓H␈↓ε        (T (CLASS1 (CDR U) (CONS (CONS 4 (CAR U)) V)))))
␈↓ ↓H␈↓␈↓ ¬pAppendix  A␈↓ %vii


␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP CLASS2
␈↓ ↓H␈↓ε (LAMBDA(U V FLG)
␈↓ ↓H␈↓ε  (COND ((NULL U) V)
␈↓ ↓H␈↓ε        ((AND FLG (= (CAAR U) 4)) (CLASS2 (CDR U) (CONS (CONS 5 (CDAR U)) V) NIL))
␈↓ ↓H␈↓ε        (T (CLASS2 (CDR U) (CONS (CAR U) V) FLG))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP MKJRST
␈↓ ↓H␈↓ε (LAMBDA (L) (LIST (LIST (QUOTE JRST) 0 L)))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMBOOL
␈↓ ↓H␈↓ε (LAMBDA(P M L FLG VPR)
␈↓ ↓H␈↓ε  (COND ((EQ P T) (COND (FLG (MKJRST L)) (T NIL)))
␈↓ ↓H␈↓ε        ((ATOM P) (LIST (COMPEXP P M VPR) (LIST (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE))) 1 L))))
␈↓ ↓H␈↓ε        ((EQ (CAR P) (QUOTE EQ))
␈↓ ↓H␈↓ε         (LIST (COMPLISA (CDR P) M VPR) 
␈↓ ↓H␈↓ε               (COND (FLG (QUOTE ((CAMN 1 2)))) (T (QUOTE ((CAME 1 2))))) 
␈↓ ↓H␈↓ε               (MKJRST L)))
␈↓ ↓H␈↓ε        ((EQ (CAR P) (QUOTE AND))
␈↓ ↓H␈↓ε         (COND ((NOT FLG) (COMPANDOR (CDR P) M L NIL VPR))
␈↓ ↓H␈↓ε               (T
␈↓ ↓H␈↓ε                ((LAMBDA (L1) (LIST (COMPANDOR1 (CDR P) M L1 L NIL VPR) (LIST (LIST (QUOTE LABEL) L1))))
␈↓ ↓H␈↓ε                 (GENSYM)))))
␈↓ ↓H␈↓ε        ((EQ (CAR P) (QUOTE OR))
␈↓ ↓H␈↓ε         (COND (FLG (COMPANDOR (CDR P) M L T VPR))
␈↓ ↓H␈↓ε               (T
␈↓ ↓H␈↓ε                ((LAMBDA (L1) (LIST (COMPANDOR1 (CDR P) M L1 L T VPR) (LIST (LIST (QUOTE LABEL) L1))))
␈↓ ↓H␈↓ε                 (GENSYM)))))
␈↓ ↓H␈↓ε        ((EQ (CAR P) (QUOTE NOT)) (COMBOOL (CADR P) M L (NOT FLG) VPR))
␈↓ ↓H␈↓ε        ((EQ (CAR P) (QUOTE NULL))
␈↓ ↓H␈↓ε         (LIST (COMPEXP (CADR P) M VPR) (LIST (LIST (COND (FLG (QUOTE JUMPE)) (T (QUOTE JUMPN))) 1 L))))
␈↓ ↓H␈↓ε        (T (LIST (COMPEXP P M VPR) (LIST (LIST (COND (FLG (QUOTE JUMPN)) (T (QUOTE JUMPE))) 1 L))))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPANDOR
␈↓ ↓H␈↓ε (LAMBDA(U M L FLG VPR)
␈↓ ↓H␈↓ε  (COND ((NULL U) NIL) (T (LIST (COMBOOL (CAR U) M L FLG VPR) (COMPANDOR (CDR U) M L FLG VPR)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP COMPANDOR1
␈↓ ↓H␈↓ε (LAMBDA(U M L L2 FLG VPR)
␈↓ ↓H␈↓ε  (COND ((NULL U) (MKJRST L2))
␈↓ ↓H␈↓ε        ((NULL (CDR U)) (COMBOOL (CAR U) M L2 (NOT FLG) VPR))
␈↓ ↓H␈↓ε        (T (LIST (COMBOOL (CAR U) M L FLG VPR) (COMPANDOR1 (CDR U) M L L2 FLG VPR)))))
␈↓ ↓H␈↓εEXPR)

␈↓ ↓H␈↓ε(DEFPROP FLAT
␈↓ ↓H␈↓ε (LAMBDA(U S)
␈↓ ↓H␈↓ε  (COND ((NULL U) S)
␈↓ ↓H␈↓ε        ((NULL (CAR U)) (FLAT (CDR U) S))
␈↓ ↓H␈↓ε        ((EQ (CAR U) (QUOTE LABEL)) (CONS (CADR U) S))
␈↓ ↓H␈↓ε        ((ATOM (CAR U)) (CONS U S))
␈↓ ↓H␈↓ε        (T (FLAT (CAR U) (FLAT (CDR U) S)))))
␈↓ ↓H␈↓εEXPR)
␈↓ ↓H␈↓␈↓ εH␈↓ ?i


␈↓ ↓H␈↓α␈↓ ¬=FUNCTION INDEX

␈↓ ↓H␈↓ack   34                                  ␈↓ εh␈↓nconc   116
␈↓ ↓H␈↓allpos   42, 43                           ␈↓ εh␈↓not   13
␈↓ ↓H␈↓allsol   40                               ␈↓ εh␈↓nth   139
␈↓ ↓H␈↓allsub   41                               ␈↓ εh␈↓numval   21, 38
␈↓ ↓H␈↓allsubsub   43                            ␈↓ εh␈↓omega   82
␈↓ ↓H␈↓alt   14                                  ␈↓ εh␈↓or   13
␈↓ ↓H␈↓altlis   26                               ␈↓ εh␈↓orlis   25
␈↓ ↓H␈↓and   13                                  ␈↓ εh␈↓p   140
␈↓ ↓H␈↓andlis   25                               ␈↓ εh␈↓picture   110
␈↓ ↓H␈↓append   16, 35, 105, 106                 ␈↓ εh␈↓plus   33
␈↓ ↓H␈↓assoc   21                                ␈↓ εh␈↓pos   139
␈↓ ↓H␈↓copy   139                                ␈↓ εh␈↓prina   132
␈↓ ↓H␈↓d   141                                   ␈↓ εh␈↓prinb   134
␈↓ ↓H␈↓diff   24                                 ␈↓ εh␈↓prindot   132
␈↓ ↓H␈↓differ   33                               ␈↓ εh␈↓prinlis   134
␈↓ ↓H␈↓editor   139                              ␈↓ εh␈↓prtn   107
␈↓ ↓H␈↓equal   16, 37                            ␈↓ εh␈↓prune   117
␈↓ ↓H␈↓equiv   121                               ␈↓ εh␈↓prup   28
␈↓ ↓H␈↓eval   28                                 ␈↓ εh␈↓read   134
␈↓ ↓H␈↓evcond   28                               ␈↓ εh␈↓reada   134
␈↓ ↓H␈↓evlist   28                               ␈↓ εh␈↓readdot   133
␈↓ ↓H␈↓evplus   21                               ␈↓ εh␈↓remv   117
␈↓ ↓H␈↓evtimes   21                              ␈↓ εh␈↓rev1   134
␈↓ ↓H␈↓fact   18, 31, 32                         ␈↓ εh␈↓reverse   17, 35, 104
␈↓ ↓H␈↓factorial   32                            ␈↓ εh␈↓reversea   17
␈↓ ↓H␈↓fib   34                                  ␈↓ εh␈↓ro   140
␈↓ ↓H␈↓fibon   121                               ␈↓ εh␈↓rt   140
␈↓ ↓H␈↓flatten   17, 37                          ␈↓ εh␈↓search   39
␈↓ ↓H␈↓fringe   17, 37                           ␈↓ εh␈↓searchlis   39
␈↓ ↓H␈↓gcd   18                                  ␈↓ εh␈↓size   37
␈↓ ↓H␈↓greaterp   33                             ␈↓ εh␈↓subst   16, 36
␈↓ ↓H␈↓i   141                                   ␈↓ εh␈↓successors   39
␈↓ ↓H␈↓indent   145                              ␈↓ εh␈↓tack   42
␈↓ ↓H␈↓insertb   116, 117                        ␈↓ εh␈↓ter   39
␈↓ ↓H␈↓last   15, 35                             ␈↓ εh␈↓trace   144
␈↓ ↓H␈↓left   119                                ␈↓ εh␈↓untrace   144
␈↓ ↓H␈↓length   20, 35                           ␈↓ εh␈↓up   140
␈↓ ↓H␈↓li   140
␈↓ ↓H␈↓loop   34, 82
␈↓ ↓H␈↓lose   39
␈↓ ↓H␈↓mapapp   40
␈↓ ↓H␈↓mapcar   23
␈↓ ↓H␈↓maplist   24
␈↓ ↓H␈↓match   41
␈↓ ↓H␈↓member   16, 35
␈↓ ↓H␈↓mod   18